๐ด The Error You're Seeing
Confirm this matches your console output. If it does, you're in the right place.
2026-02-14 11:30:45.001 WARN 8842 --- [nio-8080-exec-3] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported]โก Quick Fix Works 80% of the time
Change the @GetMapping annotation to @PostMapping on your controller method.
@PostMapping("/users")
public ResponseEntity<?> createUser(@RequestBody UserRequest request) { ... }๐ ๏ธ Solutions (3 Ways to Fix)
Match the mapping annotation to the HTTP verb
๐ Use this when the client is sending the correct HTTP method, but your controller is mapped to a different one.
If you are creating data, use @PostMapping. If you are reading, use @GetMapping. Ensure the annotation matches the request.
// Change this:
// @GetMapping("/users")
// To this:
@PostMapping("/users")
public ResponseEntity<?> createUser(@RequestBody UserRequest request) { ... }Fix the URL path in the client request
๐ Use this if you are hitting the wrong URL, which causes Spring to match a different method that doesn't support your HTTP verb.
Sometimes a typo in the URL (e.g., /user instead of /users) routes the request to a different controller that only has a GET method, resulting in a 405.
// Ensure client URL matches server path exactly
// Client: POST http://localhost:8080/api/users
// Server: @PostMapping("/api/users")Handle the exception globally
๐ Use this to return a clean JSON error instead of Spring's default 405 HTML response.
Create a global exception handler to catch HttpRequestMethodNotSupportedException.
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public ResponseEntity<String> handleMethodNotSupported(HttpRequestMethodNotSupportedException ex) {
return ResponseEntity.status(HttpStatus.METHOD_NOT_ALLOWED).body("HTTP method " + ex.getMethod() + " is not supported for this endpoint.");
}
}๐ Version Notes
Returns 405 status with default error page.
Returns 405 status, RFC 7807 ProblemDetail by default.
๐ง Why this Happens
Tap to expand the deep technical explanation
The client sent an HTTP request using a specific verb (like POST, PUT, DELETE), but the corresponding controller method mapping only allows a different verb (like GET). Spring refuses to process the request because the action is not allowed on that specific URL.
The HITEC City Parking Spot Analogy:
It is like pushing a door that says 'Pull'. The door (endpoint) exists, but you are using the wrong physical action (HTTP method) to interact with it.
๐ก๏ธ How to Prevent This Next Time
Follow REST conventions strictly: GET for reading, POST for creating, PUT/PATCH for updating, DELETE for deleting.