【发布时间】:2015-12-05 10:39:55
【问题描述】:
Spring Boot MVC 怎么会不支持 POST 方法?!我正在尝试实现一个接受实体列表的简单 post 方法:这是我的代码
@RestController(value="/backoffice/tags")
public class TagsController {
@RequestMapping(value = "/add", method = RequestMethod.POST)
public void add(@RequestBody List<Tag> keywords) {
tagsService.add(keywords);
}
}
像这样点击这个网址:
http://localhost:8090/backoffice/tags/add
请求正文:
[{"tagName":"qweqwe"},{"tagName":"zxczxczx"}]
我收到:
{
"timestamp": 1441800482010,
"status": 405,
"error": "Method Not Allowed",
"exception": "org.springframework.web.HttpRequestMethodNotSupportedException",
"message": "Request method 'POST' not supported",
"path": "/backoffice/tags/add"
}
编辑:
调试 Spring Web 请求处理程序
public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.checkRequest(request);
protected final void checkRequest(HttpServletRequest request) throws ServletException {
String method = request.getMethod();
if(this.supportedMethods != null && !this.supportedMethods.contains(method)) {
throw new HttpRequestMethodNotSupportedException(method, StringUtils.toStringArray(this.supportedMethods));
} else if(this.requireSession && request.getSession(false) == null) {
throw new HttpSessionRequiredException("Pre-existing session required but none found");
}
}
supportedMethods 中仅有的两个方法是{GET,HEAD}
【问题讨论】:
-
更新TagsController的代码。
-
考虑回滚到修订版 2。现在,您问题中的代码是正确的,因此答案毫无意义。
-
@RafalG。有问题的代码不应该是“固定的”,它打破了这个问题。
标签: java spring spring-boot