@GetMapping is a Spring annotation that maps HTTP GET requests to a controller method. It is a shortcut for @RequestMapping(method = RequestMethod.GET), introduced in Spring Framework 4.3, and it is the standard way to expose read-only endpoints in Spring MVC and Spring Boot REST APIs. The URL, query parameters, headers and path variables of the request are bound to the method's arguments.
This article covers the annotation's attributes, how to read request data, how to return correct status codes, and what changes in Spring Boot 3.
@GetMapping doesproduces@GetMapping does@GetMapping is meta-annotated with @RequestMapping(method = RequestMethod.GET). Spring registers each annotated method in a handler mapping, and the DispatcherServlet calls the method when a GET request matches the path and any extra conditions.
A request to GET /hello returns the string as the response body, because @RestController includes @ResponseBody. In a plain @Controller, the returned string would be resolved as a view name instead.
| Attribute | Meaning |
|---|---|
value / path | One or more URL patterns, such as /users/{id}. |
params | Require, or forbid, certain query parameters, for example params = "q". |
headers | Require a request header, for example headers = "X-Api-Version=2". |
consumes | Content types the request body may have. Rarely used for GET. |
produces | Content types the method can return, matched against the Accept header. |
name | A name for the mapping, used when building links. |
Unlike @RequestMapping, there is no method attribute, since GET is implied.
Spring binds method parameters from the request through annotations.
@PathVariable reads values from the URL path, such as the id in /api/users/7.@RequestParam reads query parameters like ?page=2. It is required by default, so give it a defaultValue, required = false or an Optional.@RequestHeader reads a header value.Since Spring Framework 6.1, parameter names are no longer discovered from bytecode by default, so compile with the -parameters flag (Spring Boot's Maven and Gradle plugins set it) or name the parameter explicitly, for example @PathVariable("id").
200 OK with the resource when found.404 Not Found when it is not; ResponseEntity.notFound() or throwing a ResponseStatusException both work.400 Bad Request for malformed parameters; Spring returns this automatically when a type conversion fails.304 Not Modified when you support conditional requests with ETag.Controller methods can also return records, collections or Optional values. Jackson serialises them to JSON when it is on the classpath, which Spring Boot starters include.
producesYou can serve several formats from one path and let the Accept header decide.
A client that asks for a type neither method produces receives 406 Not Acceptable.
@RequestMapping for a shared prefix instead of repeating it on every method.MockMvc lets you test the mapping without starting a server.
@MockitoBean is available from Spring Framework 6.2 and replaces the older @MockBean in Spring Boot 3.4 and later.
@GetMapping and @RequestMapping?@RequestMapping can map any HTTP method, so you set method yourself. @GetMapping fixes the method to GET and is shorter and clearer.
@GetMapping accept a request body?Technically HTTP allows it, but many clients, proxies and caches drop it. Send filters as query parameters, or use POST for complex searches.
Pass an array: @GetMapping({"/a", "/b"}).
The path matched but the request used another HTTP method. Send a GET or add a mapping for that method.
@GetMapping maps GET requests to a handler method and replaces @RequestMapping(method = GET). Bind data with @PathVariable, @RequestParam and @RequestHeader, return meaningful status codes, and keep GET handlers free of side effects.