Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 Client add-on for Chrome.

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

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

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.

HTML Page Below:

<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;
    }
}
like image 912
Sachin Jain Avatar asked Apr 08 '16 11:04

Sachin Jain


People also ask

What is request method GET not supported?

The Request Method' POST' Not Supported error is caused by a mismatch of the web browser configuration and the browser's URL format. In this case, the browser sends a URL request, the web server receives and recognizes the URL but cannot execute commands or grant access to the requested page.

How does spring boot handle method not supported exception?

405 Not Support – Reason, Solution As we can expect, we can solve this by defining an explicit mapping for PUT in the existing method mapping: @RequestMapping( value = "/employees", produces = "application/json", method = {RequestMethod. GET, RequestMethod.

What are the HTTP methods that Cannot be implemented in spring boot REST service?

Return 405 (Method Not Allowed), unless you want to replace every resource in the entire collection of resource - use with caution. 404 (Not Found) - if id not found or invalid and creating new resource is not allowed. Return 405 (Method Not Allowed), unless you want to modify the collection itself..

CAN GET request have body in spring boot?

In this article, we will discuss how to get the body of the incoming request in the spring boot. @RequestBody: Annotation is used to get request body in the incoming request. Note: First we need to establish the spring application in our project. Step 2: Click on Generate which will download the starter project.


2 Answers

I guess the solution is described here: 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(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()));
}
like image 108
zypro Avatar answered Oct 02 '22 16:10

zypro


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

    @DeleteMapping(path="/something")
    @PostMapping(path="/something")
    @GetMapping(path="/something")
like image 44
mayank verma Avatar answered Oct 02 '22 15:10

mayank verma