Spring Boot @PostMapping Example

1. Introduction

The @PostMapping annotation in Spring Boot simplifies the handling of HTTP POST requests. It is a specialized version of the @RequestMapping annotation that narrows the request mapping to HTTP POST methods, which are commonly used for submitting form data.

Key Points:

1. @PostMapping is a composed annotation that is used to handle POST type requests in a web application.

2. It is typically used to handle form submissions or to accept JSON payloads.

3. @PostMapping can be configured to accept various content types and produce specific responses.

2. Implementation Steps

1. Create a new Spring Boot project with the necessary web dependencies.

2. Define a controller class and annotate it with @RestController.

3. Inside the controller, create a method and annotate it with @PostMapping.

4. Define the endpoint URL, request, and response body within the method.

5. Start the application and test the POST request endpoint.

3. Implementation Example

Here is the complete code that demonstrates the usage of @PostMapping annotation:
// Step 2: Define a REST controller to handle web requests
@RestController
public class UserController {

    // Step 3: Use @PostMapping to handle POST requests for /users
    @PostMapping("/users")
    public User createUser(@RequestBody User user) {
        // Step 4: Here, you would typically save the user to the database
        // For this example, we'll just return the user object
        return user;
    }
}

// Step 3: Define a User class as a simple domain model
class User {
    private String username;
    private String email;

    // Standard getters and setters
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

// Step 5: Create the main application class to bootstrap the application
@SpringBootApplication
public class PostMappingExampleApplication {

    public static void main(String[] args) {
        SpringApplication.run(PostMappingExampleApplication.class, args);
    }
}

Output:

{
    "username": "johndoe",
    "email": "john.doe@example.com"
}

Explanation:

1. @RestController above the UserController class marks it as a controller where every method returns a domain object as a JSON response.

2. @PostMapping("/users") specifies that the createUser method will handle HTTP POST requests to the /users endpoint.

3. @RequestBody User user indicates that the method should expect a JSON object in the request body which will be deserialized into a User object.

4. The createUser method returns the User object, which will be automatically serialized to JSON and sent in the response body.

5. PostMappingExampleApplication is the main class marked with @SpringBootApplication, responsible for running the application.

6. The output is a JSON representation of the User object created from the POST request's body, demonstrating a successful data submission.


Comments