Java Spring Boot Rest 服务 | 不支持请求方法“GET”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36498393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:56:33  来源:igfitidea点击:

Spring Boot Rest Service | Request method 'GET' not supported

javajqueryhtmlrestspring-boot

提问by Sachin Jain

I have made a hello world rest service using spring boot. I am able to call the service via standalone java program. I am also able to call it via Advanced Rest Clientadd-on for Chrome.

我使用 spring boot 做了一个 hello world rest 服务。我可以通过独立的 Java 程序调用该服务。我还可以通过Chrome 的Advanced Rest Client插件调用它。

But when I try to hit it via a standalone HTML page using jQuery AJAX I am getting error

但是当我尝试使用 jQuery AJAX 通过独立的 HTML 页面点击它时,我收到错误

WARN 3748 --- [nio-9000-exec-2] o.s.web.servlet.PageNotFound : Request method 'GET' not supported

警告 3748 --- [nio-9000-exec-2] osweb.servlet.PageNotFound:不支持请求方法“GET”

Any Help is appreciated.

任何帮助表示赞赏。

PS: I am assuming that as I am able to call the WebService using other modes so the controller is fine. But something is wrong the way I am calling it from the HTML.

PS:我假设因为我能够使用其他模式调用 WebService,所以控制器很好。但是我从 HTML 调用它的方式有问题。

HTML Page Below:

下面的 HTML 页面:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>

<head>
  <meta charset="ISO-8859-1">
  <title>Client</title>
  <script type="text/javascript" src="jquery-2.2.3.js">
  </script>
  <script type="text/javascript">
    $(document).ready(function() {
      $("#Submit").click(function() {
        var input = {
          "name": $("#name").val(),
          "language": $("#language").val()
        };
        var inputStr = JSON.stringify(input);
        alert(inputStr);
        $.ajax({
          url: "http://localhost:9000/rest/greetMeObj/",
          method: "POST",
          data: inputStr,
          dataType: "jsonp",
          success: function(output) { // callback method for further manipulations
            var str = JSON.stringify(output);
            $("#output").text(data);
          },
          error: function(data) { // if error occured
            $("#error").text(data);
          }
        });

      });
    });
  </script>
</head>

<body>
  <div id="input">
    <label><b>Name:</b>
    </label>
    <input type="text" name="name" id="name" alt="Enter you name" title="Enter your name" />
    <br />
    <br />
    <label><b>Language:</b>
    </label>
    <select name="language" id="language" title="Select your language">
      <option value="en" label="English" selected="selected">English</option>
      <option value="fr" label="French">French</option>
      <option value="nl" label="Dutch">Dutch</option>
    </select>
    <br />
    <br />
    <button title="Submit" type="button" name="Submit" id="Submit" value="Submit" formaction="POST">Submit</button>
  </div>
  <div id="output"></div>
  <div id="error" style="color: red;"></div>
</body>

</html>

Spring Controller Below

下面的弹簧控制器

@Controller
@RequestMapping("/rest/*")
public class GreetingController {

    private static final String TEMPLATE_EN = "Hello, %s!";
    private static final String TEMPLATE_FR = "Bonjour, %s!";
    private static final String TEMPLATE_NL = "Hallo, %s!";
    private final AtomicLong counter = new AtomicLong();

    @RequestMapping(value="/rest/greetMe", method= RequestMethod.GET)
    public @ResponseBody Greeting sayHello(
            @RequestParam(value = "name", required = false, defaultValue = "Stranger") String name,
            @RequestParam(value = "language", required = false, defaultValue = "en") String language) {
        return new Greeting(counter.incrementAndGet(), String.format(getTemplate(language), name));
    }

    @RequestMapping(value="/rest/greetMeObj", method= RequestMethod.POST)
    public @ResponseBody Greeting sayHello(
            @RequestBody(required = true) Input input) {
        return new Greeting(counter.incrementAndGet(),
                String.format(getTemplate(input.getLanguage()), input.getName()));
    }

    private String getTemplate(String language) {
        String template;
        switch (language) {
        case "nl":
        case "NL":
            template = TEMPLATE_NL;
            break;
        case "fr":
        case "FR":
            template = TEMPLATE_FR;
            break;
        case "en":
        case "EN":
        default:
            template = TEMPLATE_EN;
            break;
        }
        return template;
    }
}

采纳答案by zypro

I guess the solution is described here: Why SpringMVC Request method 'GET' not supported?

我想这里描述了解决方案:Why SpringMVC Request method 'GET' not supported?

Both values in the RequestMapping has to be the same. So for each value you have to define one for GET and one for POST.

RequestMapping 中的两个值必须相同。因此,对于每个值,您必须为 GET 定义一个,为 POST 定义一个。

@RequestMapping(value="/rest/greetMe", method= RequestMethod.GET)
public @ResponseBody Greeting sayHello(
        @RequestParam(value = "name", required = false, defaultValue = "Stranger") String name,
        @RequestParam(value = "language", required = false, defaultValue = "en") String language) {
    return new Greeting(counter.incrementAndGet(), String.format(getTemplate(language), name));
}

@RequestMapping(value="/rest/greetMe", method= RequestMethod.POST)
public @ResponseBody Greeting sayHello(
        @RequestBody(required = true) Input input) {
    return new Greeting(counter.incrementAndGet(),
            String.format(getTemplate(input.getLanguage()), input.getName()));
}

回答by mayank verma

Check your controller, if you have mapped any of these calls to default mapping:

检查您的控制器,如果您已将这些调用中的任何一个映射到默认映射:

    @DeleteMapping()
    @PostMapping()
    @GetMapping()

It is mapped to path="/". Please change it to

它被映射到path="/". 请改成

    @DeleteMapping(path="/something")
    @PostMapping(path="/something")
    @GetMapping(path="/something")