【问题标题】:Spring MVC HTTP Status 405 - Request method 'POST' not supported - Backbone RequestSpring MVC HTTP 状态 405 - 不支持请求方法“POST” - 主干请求
【发布时间】:2013-11-25 19:03:30
【问题描述】:

我是 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)

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

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

我已经尝试按照其他 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>  

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

我把我的 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);
    }

}

非常感谢!

【问题讨论】:

    标签: json rest spring-mvc backbone.js


    【解决方案1】:

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

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

    @RequestMapping(method = RequestMethod.POST )
    

    【讨论】:

    • 是的!这就是问题所在。你知道为什么会这样吗?谢谢;)
    • 我没有查看代码,但我怀疑它与哪个请求映射值与实际请求路径匹配有关。为避免将来出现这种情况,最好不要真正映射到“/”,而是将其留空。
    【解决方案2】:

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

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

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2012-06-24
        • 2015-04-09
        • 2016-03-29
        • 2014-09-24
        • 2013-04-09
        • 2012-04-20
        • 2020-07-24
        • 2015-05-01
        • 2017-05-03
        相关资源
        最近更新 更多