小编典典

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

spring-boot

我已经使用Spring Boot进行了hello world
rest服务。我可以通过独立的Java程序调用该服务。我也可以通过Chrome的Advanced Rest
Client
插件来调用它。

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

WARN 3748 — [nio-9000-exec-2] osweb.servlet.PageNotFound:请求方法’GET’不支持

任何帮助表示赞赏。

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

下面的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>

下方的弹簧控制器

@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;
    }
}

阅读 306

收藏
2020-05-30

共1个答案

小编典典

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()));
}
2020-05-30