Java
Spring Boot
Annotations
GetMapping
RequestMapping

Difference between the annotations GetMapping and RequestMappingmethod RequestMethod.GET

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In Spring MVC and Spring Boot controllers, @GetMapping and @RequestMapping(method = RequestMethod.GET) both map HTTP GET requests to a handler method. The practical difference is not behavior in normal use, but intent: @GetMapping is the concise, GET-specific form, while @RequestMapping is the more general mapping annotation that can describe many kinds of request rules.

They Mean The Same HTTP Method

At the method level, these two handlers are equivalent in purpose.

java
1@GetMapping("/users")
2public String listUsers() {
3    return "users";
4}
java
1@RequestMapping(value = "/users", method = RequestMethod.GET)
2public String listUsers() {
3    return "users";
4}

Both map GET requests for /users to the same controller method. If all you need is a normal GET endpoint, @GetMapping is usually clearer because the HTTP method is visible immediately.

Why @GetMapping Exists

@GetMapping is a composed shortcut annotation. It exists to reduce boilerplate and make controller methods easier to scan.

When a controller has many endpoints, a row of @GetMapping, @PostMapping, @PutMapping, and @DeleteMapping annotations is easier to understand than repeating @RequestMapping(method = ...) everywhere.

That readability benefit is the main reason most modern Spring code prefers @GetMapping for simple GET handlers.

What @RequestMapping Still Does Better

@RequestMapping is still important because it is the base annotation for request mapping rules in Spring. It is often used at the class level to define a shared path prefix.

java
1import org.springframework.web.bind.annotation.GetMapping;
2import org.springframework.web.bind.annotation.RequestMapping;
3import org.springframework.web.bind.annotation.RestController;
4
5@RestController
6@RequestMapping("/api/users")
7public class UserController {
8
9    @GetMapping
10    public String listUsers() {
11        return "all users";
12    }
13
14    @GetMapping("/active")
15    public String activeUsers() {
16        return "active users";
17    }
18}

This is a common pattern: use @RequestMapping once on the class, then use the HTTP-specific annotations on individual methods.

Shared Attributes

The two forms can express the same mapping constraints for a GET route, including path, query parameter requirements, headers, consumes, and produces.

java
1@GetMapping(value = "/report", produces = "application/json", params = "full=true")
2public String fullReport() {
3    return "full report";
4}
java
1@RequestMapping(
2    value = "/report",
3    method = RequestMethod.GET,
4    produces = "application/json",
5    params = "full=true"
6)
7public String fullReport() {
8    return "full report";
9}

So the decision is usually about clarity, not capability.

When To Prefer Each One

Use @GetMapping when the method handles only GET requests. That is the ordinary choice for REST read endpoints and page-loading handlers.

Use @RequestMapping when you need the generic form, especially at the class level or in legacy code where a controller uses one style consistently. It is also the annotation you reach for when teaching how Spring request mapping works at a lower level.

Common Pitfalls

A common mistake is assuming @GetMapping is more powerful or more modern in behavior than @RequestMapping(method = RequestMethod.GET). For a GET handler, it is mostly a cleaner wrapper around the same idea.

Another mistake is mixing mapping styles inconsistently in the same controller without a reason. That makes the codebase harder to scan. Pick a pattern and keep it consistent.

It is also easy to forget that @RequestMapping without method = RequestMethod.GET is not GET-specific at all. If you omit the method attribute, the handler may match more broadly than intended.

Summary

  • '@GetMapping and @RequestMapping(method = RequestMethod.GET) both map GET requests.'
  • '@GetMapping is the shorter, clearer form for GET-only handler methods.'
  • '@RequestMapping is the general-purpose annotation and is still common at the class level.'
  • Both forms support path, headers, params, and media-type conditions.
  • Prefer @GetMapping for simple GET endpoints unless a broader @RequestMapping style is needed.

Course illustration
Course illustration

All Rights Reserved.