json Spring MVC HTTP 状态 405 - 不支持请求方法“POST” - 主干请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19949373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 20:02:56  来源:igfitidea点击:

Spring MVC HTTP Status 405 - Request method 'POST' not supported - Backbone Request

jsonrestspring-mvcbackbone.js

提问by jantunes

I am new at Spring MVC and I am trying to build a Web Application from scratch using Spring MVC + Hibernate to serve something like a JSON Rest API, having this API consumed through Backbone at the client side. To do that I have started following this tutorial ( http://www.mkyong.com/spring-mvc/spring-3-mvc-and-json-example/) .

我是 Spring MVC 的新手,我正在尝试使用 Spring MVC + Hibernate 从头开始​​构建一个 Web 应用程序,以提供类似 JSON Rest API 的服务,并通过客户端的 Backbone 使用此 API。为此,我已开始遵循本教程 ( http://www.mkyong.com/spring-mvc/spring-3-mvc-and-json-example/)。

So I have a model Message which will have the following REST API Interface:
GET /api/messages               ( working ok )
GET /api/messages/:id           ( working ok )
DELETE /api/messages/:id        ( working ok )
PUT /api/messages/:id           ( working ok )
POST /api/messages              ( error: (DefaultHandlerExceptionResolver.java:194) - Request method 'POST' not supported)

I expected this problem happens for PUT or DELETE requests when doing the request through a form, but not for a POST request. I am not even doing the request through a form. On the client side the request is done through Backbone like this:

我预计通过表单执行请求时 PUT 或 DELETE 请求会发生此问题,但不会发生 POST 请求。我什至没有通过表格提出请求。在客户端,请求通过 Backbone 完成,如下所示:

new App.Models.Message({ attributeA : 'a', attributeB : 'b' }).save();

I have already tried to add the httpMethodFilter at web.xml as suggested in other Stackoverflow questions:

我已经尝试按照其他 Stackoverflow 问题中的建议在 web.xml 添加 httpMethodFilter :

 <filter>
    <filter-name>httpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
 </filter>

 <filter-mapping>
    <filter-name>httpMethodFilter</filter-name>
    <servlet-name>mvc-dispatcher</servlet-name>
 </filter-mapping>  

Has anyone had the same problem?

有没有人遇到过同样的问题?

I leave here my MessagesController:

我把我的 MessagesController 留在这里:

@Controller
@RequestMapping("/api/messages")
public class MessagesController {

    @Autowired  
    private MessageService messageService;

    @RequestMapping(method = RequestMethod.GET)
    public @ResponseBody List<Message> getMessagesInJSON(@RequestParam( value = "type", required = false ) String type) {   
        List<Message> messages = messageService.findAll();          
        return messages; 
    }

    @RequestMapping( value = "/{id}", method = RequestMethod.GET )
    public @ResponseBody Message getMessageInJson(@PathVariable Integer id ) {      
        Message message = messageService.findById(id);
        return message;     
    }

    @RequestMapping( value = "/{id}", method = RequestMethod.DELETE )
    @ResponseStatus( value = HttpStatus.NO_CONTENT )
    public void deleteMessage(@PathVariable Integer id ) throws NotFoundException {     
        messageService.delete(id);      
    }

    @RequestMapping( value = "/{id}", method = RequestMethod.PUT )
    @ResponseStatus( value = HttpStatus.NO_CONTENT )
    public void editMessage( @PathVariable Integer id, @RequestBody Message message ) throws NotFoundException {
        message.setId(id);
        messageService.update(message);
    }

    @RequestMapping( value = "/", method = RequestMethod.POST )
    @ResponseStatus(HttpStatus.CREATED)
    public @ResponseBody Message createMessage( @RequestBody Message message ) {        
        return messageService.create(message);
    }

}

Thanks a lot!

非常感谢!

回答by Bart

You have already mapped /api/messagesto the getMessagesInJSONmethod which only allows a GET request. Your POST request is mapping to a different path.

您已经映射/api/messagesgetMessagesInJSON只允许 GET 请求的方法。您的 POST 请求正在映射到不同的路径。

I suggest to omit the value attribute on your request mapping for createMessage.

我建议在您的请求映射中省略 value 属性createMessage

@RequestMapping(method = RequestMethod.POST )

回答by CodeMed

I think the problem is that your @RequestMappingis located at the top of your class definition, and conflicts with the strings that you associated with the valueargument in the other @RequestMappingannotations in front of each method. By contrast, I would start the class with the following:

我认为问题在于您@RequestMapping位于类定义的顶部,并且与您在每个方法前面value的其他@RequestMapping注释中与参数关联的字符串冲突。相比之下,我会用以下内容开始课程:

@Controller
@SessionAttributes(types = SomeClassName.class)
public class SomeClassNameController {  

Then you can use the valueparameter in the @RequestMappingannotations that you have in front of each method.

然后,您可以在每个方法前面value@RequestMapping注释中使用该参数。

回答by amertkara

It is also good to keep in mind that this error might happen if you are implementing /errorendpoint for exception handling. If your /errorendpoint has a method type and it doesn't match the method of the endpoint where the exception was thrown; you might end up getting 405. If that is the case, remove method type from the error endpoint to serve to all.

请记住,如果您正在实现/error用于异常处理的端点,则可能会发生此错误。如果您的/error端点具有方法类型并且它与抛出异常的端点的方法不匹配;您最终可能会得到 405。如果是这种情况,请从错误端点中删除方法类型以提供给所有人。