The 30-Second Fix
The HttpRequestMethodNotSupportedException happens when your request type (like POST) doesn't match the one defined in your Spring Controller (like GET). Here is your checklist to fix it immediately:
- Check the annotation: Use
@PostMappingif you are sending data. - Watch the URL: You might be hitting a GET-only endpoint by mistake.
- Verify the client: Ensure Postman, cURL, or your frontend isn't defaulting to GET.
Why is this happening?
Spring MVC relies on a HandlerMapping to direct incoming traffic to your @RestController. It acts like a traffic controller that checks two things: the URL path and the HTTP verb (GET, POST, PUT, etc.).
When this exception pops up, Spring has actually found a matching URL. The problem is that the specific method you're calling isn't set up to handle the HTTP verb you sent. For instance, if you call a @GetMapping endpoint with a POST request, Spring will block it. It then throws this exception and returns a 405 Method Not Allowed status code to the client.
Where things usually go wrong
1. Mismatched Annotations
This is the culprit in about 90% of cases. You might intend to save data but accidentally use the wrong mapping annotation.
The broken code:
@RestController
@RequestMapping("/api/v1/users")
public class UserController {
// This endpoint only listens for GET
@GetMapping("/save")
public ResponseEntity<String> saveUser(@RequestBody User user) {
return ResponseEntity.ok("User saved");
}
}
If you send a POST to /api/v1/users/save, it fails. Why? Because @GetMapping only allows reading data, not posting it.
The fixed code:
@RestController
@RequestMapping("/api/v1/users")
public class UserController {
@PostMapping("/save") // Now it accepts POST requests
public ResponseEntity<String> saveUser(@RequestBody User user) {
return ResponseEntity.status(201).body("User created with ID: 101");
}
}
2. Typos and Path Variables
Small typos in the URL can lead Spring to the wrong method. Consider these two endpoints:
GET /api/products/{id}(Fetch one product)POST /api/products(Create a new product)
If you accidentally call POST /api/products/123, Spring matches the path to the GET method. It then sees you are trying to use POST on an ID-specific path and throws the exception because that specific route doesn't support POST.
3. CSRF Protection in Spring Security
Spring Security enables Cross-Site Request Forgery (CSRF) protection by default. Usually, this triggers a 403 Forbidden error. However, custom security filters or internal redirects can sometimes mask the real issue, resulting in a confusing 405 error instead.
If you are building a stateless REST API using JWT, you can safely disable CSRF in your configuration:
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.csrf(csrf -> csrf.disable())
.authorizeHttpRequests(auth -> auth.anyRequest().permitAll());
return http.build();
}
How to verify the fix
Don't rely on the browser to test this, as browsers often cache results or default to GET. Use curl to see exactly what the server returns.
Run this command in your terminal:
curl -i -X POST http://localhost:8080/api/v1/users/save \
-H "Content-Type: application/json" \
-d '{"name": "Alice", "email": "alice@example.com"}'
A successful fix will return a 200 OK or 201 Created. If it still fails, the -i flag will show you the exact headers and status code returned by the server.
Reading the logs like a pro
Check your console output. Spring provides a clear hint when it resolves this exception:
WARN 45210 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported. Available methods: [GET]]
This log is a goldmine. It explicitly lists the Available methods. If it says [GET], you know for a fact your controller is missing the @PostMapping annotation.
Next Steps
- Review the Spring @RequestMapping docs.
- Ensure your frontend fetch/axios calls specify the correct
methodproperty. - Standardize your API paths to avoid confusion between resource collections and individual items.

