我的spring boot REST控制器中有2个方法
这是我的REST CONTROLLER代码
@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";
}
}
这是我的CONTROLLER代码
@Controller
public class MainController {
@RequestMapping(value = "/",method=RequestMethod.GET)
public String getMainPage(){
return "index.html";
}
}
现在问题是我点击PATCH请求 http://localhost:8080/ 它返回
{
"timestamp": 1486041782895,
"status": 405,
"error": "Method Not Allowed",
"exception":"org.springframework.web.HttpRequestMethodNotSupportedException",
"message": "Request method 'PATCH' not supported",
"path": "/"
}
当我点击GET请求http://localhost:8080/时,它会返回
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
任何人都可以告诉我原因吗?
答案 0 :(得分:0)
现在问题是当我点击PATCH请求http://localhost:8080/时它返回
{
"timestamp": 1486041782895,
"status": 405,
"error": "Method Not Allowed",
"exception":"org.springframework.web.HttpRequestMethodNotSupportedException",
"message": "Request method 'PATCH' not supported",
"path": "/"
}
发生此错误是因为您声明了一种方法,该方法需要方法中的PathVariable。
如果您希望它在不传递变量的情况下工作,您应该执行以下操作:
@RequestMapping(method=RequestMethod.PATCH)
public @ResponseBody String patch(@RequestParam(name = "name", required = false) String name){
return "ANOTHER PATCH";
}
所以这个PATCH请求http://localhost:8080/应该返回"另一个补丁"
这是你的问题吗?