Technical Analysis of Resolving 405 Method Not Allowed Error for PUT and POST Requests in Spring MVC

Dec 08, 2025 · Programming · 13 views · 7.8

Keywords: Spring MVC | RESTful Web Services | 405 Method Not Allowed Error | PUT Requests | @ResponseBody Annotation

Abstract: This article delves into the common causes and solutions for the 405 Method Not Allowed error encountered with PUT and POST requests when developing RESTful Web services using the Spring MVC framework. Through an analysis of a real-world case, it explains request header configuration, controller method annotations, and server response mechanisms, focusing on how to properly configure PUT methods by adjusting @Consumes and @ResponseBody annotations. Additionally, the article supplements other potential error sources, such as Content-Type mismatches and server configuration issues, providing developers with a comprehensive debugging and resolution approach.

Problem Background and Error Phenomenon

When developing RESTful Web services based on Spring MVC, developers often encounter 405 Method Not Allowed errors for PUT and POST requests, while GET and DELETE requests work correctly. This error typically manifests in the HTTP response header with the Allow field listing only GET and DELETE methods, e.g., Allow: GET, DELETE. This clearly indicates that the server is not configured to handle PUT and POST requests.

Core Error Analysis

From the provided case, the root cause lies in improper configuration of controller methods. In the initial code, the PUT method used @RequestMapping(value="/{id}",method = RequestMethod.PUT, consumes = "Application/json") but lacked the @ResponseBody annotation. This prevented Spring MVC from correctly serializing responses, potentially triggering internal server configuration issues that disallow PUT methods.

Detailed Solution

The optimal solution is to modify the PUT method by removing the consumes = "Application/json" parameter and adding the @ResponseBody annotation. The revised code is as follows:

@RequestMapping(value="/{id}",method = RequestMethod.PUT)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public void updateUser(@PathVariable int id, @RequestBody User temp){
    // Implement update logic
}

This adjustment ensures the method properly handles JSON request bodies and, through the @ResponseBody annotation, allows Spring MVC to manage responses correctly, thereby permitting PUT requests. Experiments confirm that this modification effectively resolves the 405 error.

Supplementary Causes and Preventive Measures

Beyond the core issue, other factors may contribute to 405 errors. For instance, if the request header's Content-Type is set to application/x-www-form-urlencoded while the server expects application/json, a mismatch can occur. Developers should ensure the client-sent Content-Type aligns with server-side expectations in @Consumes or @RequestBody. Additionally, server configurations, such as WebLogic Servlet settings, might restrict certain HTTP methods, requiring checks against server documentation or configuration files.

Conclusion and Best Practices

The key to resolving 405 Method Not Allowed errors is meticulous inspection of controller method annotations and request header alignment. It is recommended that developers consistently use the @ResponseBody annotation to ensure response serialization and verify Content-Type consistency when implementing RESTful services. Through systematic debugging and adherence to Spring MVC best practices, such common errors can be effectively avoided, enhancing the reliability and compatibility of Web services.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.