0
0
SpringbootDebug / FixBeginner · 4 min read

How to Handle POST Request in Spring Boot: Simple Guide

To handle a POST request in Spring Boot, create a controller method annotated with @PostMapping and use @RequestBody to receive the request data. This method processes the input and returns a response, enabling REST API functionality.
🔍

Why This Happens

Developers often forget to use the @PostMapping annotation or miss the @RequestBody annotation on the method parameter. This causes Spring Boot to not map the POST request correctly or fail to parse the request body, leading to errors or empty input.

java
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;

@RestController
@RequestMapping("/api")
public class UserController {

    // Missing @PostMapping and @RequestBody
    public String addUser(User user) {
        return "User added: " + user.getName();
    }
}
Output
Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. There was an unexpected error (type=Method Not Allowed, status=405). Request method 'POST' not supported
🔧

The Fix

Use @PostMapping on the controller method to map POST requests. Add @RequestBody to the method parameter to tell Spring Boot to convert the JSON request body into a Java object automatically.

java
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

@RestController
@RequestMapping("/api")
public class UserController {

    @PostMapping("/users")
    public String addUser(@RequestBody User user) {
        return "User added: " + user.getName();
    }
}

// User class
public class User {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
Output
When you POST JSON {"name":"Alice"} to /api/users, response: "User added: Alice"
🛡️

Prevention

Always annotate POST handler methods with @PostMapping and use @RequestBody for input data. Validate input objects and use tools like Spring Boot DevTools for fast feedback. Writing integration tests for your controllers helps catch mapping issues early.

⚠️

Related Errors

  • 405 Method Not Allowed: Happens when the HTTP method is not mapped; fix by adding correct mapping annotation.
  • 400 Bad Request: Occurs if JSON cannot be parsed; ensure request body matches your Java object structure.
  • 415 Unsupported Media Type: Happens if Content-Type header is missing or wrong; set it to application/json for JSON payloads.

Key Takeaways

Use @PostMapping to map POST requests in Spring Boot controllers.
Annotate method parameters with @RequestBody to receive JSON data as Java objects.
Set Content-Type header to application/json when sending POST requests with JSON.
Write tests to verify your POST endpoints handle requests correctly.
Validate input data to avoid runtime errors and improve API reliability.