0

I have 2 method in my spring boot REST controller

here is my REST CONTROLLER code

@RestController
public class MainRestController {

@RequestMapping(value = "/",method=RequestMethod.POST)
public String startMigration(){
        return "POSt";
}

@RequestMapping(value = "/",method=RequestMethod.PATCH)
public String PUT(){
        return "PUT";
}

/*
 * If i commnet this method and un comment following method it will run
 * */
@RequestMapping(value = "/{name}",method=RequestMethod.PUT)
public String PATCH(@PathVariable String name){
        return "PATCH";
}

/*@RequestMapping(value = "/",method=RequestMethod.PATCH)
public String PATCH(){
        return "PATCH";
}*/

@RequestMapping(value = "/",method=RequestMethod.DELETE)
public String DELETE(){
        return "DELETE";
}
}

and here is my CONTROLLER code

@Controller
public class MainController {

@RequestMapping(value = "/",method=RequestMethod.GET)
public String getMainPage(){
    return "index.html";
}
}

Now issue is when i hit PATCH request http://localhost:8080/ it return

{
"timestamp": 1486041782895,
"status": 405,
"error": "Method Not Allowed",
"exception":"org.springframework.web.HttpRequestMethodNotSupportedException",
"message": "Request method 'PATCH' not supported",
"path": "/"
}

and when i hit GET request http://localhost:8080/ it return

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Thu Feb 02 19:00:18 IST 2017
There was an unexpected error (type=Method Not Allowed, status=405).
Request method 'GET' not supported 

can anybody tell me the reason?

atiwari54
  • 51
  • 1
  • 2
  • 7

1 Answers1

0

Now issue is when i hit PATCH request http://localhost:8080/ it return

{
"timestamp": 1486041782895,
"status": 405,
"error": "Method Not Allowed",
"exception":"org.springframework.web.HttpRequestMethodNotSupportedException",
"message": "Request method 'PATCH' not supported",
"path": "/"
}

This error happens because you have declared a method that expects a PathVariable in the method.

If you want it to work without passing variable, you should do something like:

@RequestMapping(method=RequestMethod.PATCH)
public @ResponseBody String patch(@RequestParam(name = "name", required = false) String name){
    return "ANOTHER PATCH";
}

So this PATCH request http://localhost:8080/ should return "ANOTHER PATCH"

Was this your question?

  • My issue with GET request, when i comment my PATCH method GET start working – atiwari54 Feb 03 '17 at 14:32
  • I'm not an expert, but you probably wrote the class with Controller instead of RestController. In the RestController annotation, the ResponseBody methods are encapsulated. When using Controller, it is necessary to use the ResponseBody annotation (in the class or methods) that will automatically serialize the return value according to the external client's capabilities and the libraries available on the classpath. Perhaps some expert can explain to us better why. – Philipe Alves Feb 03 '17 at 15:41
  • Yes i have 1 controller in which i have written GET for UI(html) and one REST Controller for web services which contains PATCH method. when i give alias with requestmapping then it works properly but m not giving any alias working with only "/" then its not working – atiwari54 Feb 06 '17 at 05:42