@GetMapping vs @PostMapping in Spring Boot

In this quick guide, we will learn what are @GetMapping and @PostMapping annotations in Spring Boot and when to use them with examples.

What is Spring Boot?

Spring Boot is an extension of the Spring framework that simplifies the initial setup and development of new Spring applications. Its goal is to minimize the configuration and setup typically required in Spring, making it easier and faster to get a Spring application up and running.

HTTP GET vs HTTP POST

Before diving into the annotations, let's quickly review the HTTP methods GET and POST:

GET: This method is used to request data from a specified resource. GET requests should only retrieve data and not have any other effect on the data.

POST: This method is used to send data to a server to create/update a resource. POST requests are often used when submitting form data or uploading a file.

Using @GetMapping in Spring Boot

@GetMapping is a shortcut for @RequestMapping(method = RequestMethod.GET). It is used to map HTTP GET requests using specific handler methods.

Example of @GetMapping

Suppose we have a simple Spring Boot application where we want to retrieve a list of users. Here’s how we can use @GetMapping to achieve this:
@RestController
public class UserController {
    @GetMapping("/users")
    public List<User> getAllUsers() {
        return userService.findAllUsers();
    }
}

In this example, whenever an HTTP GET request is made to /users, the getAllUsers method will be triggered, which fetches all users from the user service.

Using @PostMapping in Spring Boot

@PostMapping is a specialized version of @RequestMapping that acts as a shortcut for @RequestMapping(method = RequestMethod.POST). It maps HTTP POST requests onto specific handler methods.

Example of @PostMapping

Let's consider a scenario where we want to add a new user. Here's how @PostMapping can be used:

@RestController
public class UserController {
    @PostMapping("/users")
    public User addUser(@RequestBody User user) {
        return userService.saveUser(user);
    }
}

In this case, when a POST request is made to /users with a user JSON object, the addUser method will be called, creating a new user in the database.

Use Cases

When to Use @GetMapping

  • Retrieving read-only data such as user profiles, posts, or articles. 
  • Fetching filtered data, such as a search result. 
  • Any server interaction where data is being fetched without any modification.

When to Use @PostMapping

  • Creating new resources like a new user, a blog post, or a comment. 
  • Any form submissions where data needs to be encapsulated, such as login forms or data entry forms. 
  • Handling file uploads.

Conclusion

In Spring Boot, @GetMapping and @PostMapping are shorthand annotations used to handle HTTP GET and POST requests, respectively: 

@GetMapping is used to map HTTP GET requests onto specific handler methods. It is typically used for retrieving data from a server. 

@PostMapping is used to map HTTP POST requests onto specific handler methods. It is generally used for submitting data to be processed (e.g., creating or updating a resource). 

Both annotations are derived from @RequestMapping but are more specific to the HTTP method they handle, making the controller methods clearer and more concise.

Comments