@RequestMapping
- Handles web requests for one or more HTTP methods(GET, POST, PUT, DELETE).
- Can be applied at both class and method levels.
Syntax
@RequestMapping(
value = "/path", // or path = "/path"
method = RequestMethod.GET, // HTTP method(s)
params = "id=10", // restrict by query parameters
headers = "key=value", // restrict by request headers
consumes = "application/json", // accepted Content-Type
produces = "application/json" // response Content-Type
)
1. Path-Only Mapping (URL-only)
if don’t specify the method attribute, it handles all HTTP method .
Handles :
2. Method-Specific Mapping (Path + HTTP Method)
Case 2.1: Same Path & Same Method
@RequestMapping(path = "/user", method = RequestMethod.GET)
@RequestMapping(path = "/user", method = RequestMethod.GET)
What happens?
Spring throws an "Ambiguous mapping" error at startup because it cannot decide which method to use.
Case 2.2: Same Path, Different Methods
@RequestMapping(path = "/user", method = RequestMethod.GET)
@RequestMapping(path = "/user", method = RequestMethod.POST)
This is valid and commonly used.Spring will map:
Case 2.3: Multiple Methods in One Mapping
Handles both GET and PUT for /user.3. Base URL Mapping
every method inside starts with /user.
@RestController
@RequestMapping("/user")
public class UserController{
@GetMapping("/profile") public String getProfile() { }
@GetMapping("/order") public String getOrder() { }
}
4. Shorthand (Method-Specific) Mappings
@GetMapping("/path") //Handles GET requests only
@PostMapping("/path") //Handles POST requests only
@PutMapping("/path") //Handles PUT requests only
@DeleteMapping("/path") //Handles DELETE requests only
@DeleteMapping(path={"/path","/pathtwo"}) //Handles DELETE requests only
5. Consumes and Produces
5.1. consumes
- Defines what Content-Type the API can accept (request body format).
- Matches
client’s Content-Type header. - Example: consumes = "application/json" → accepts only JSON input.
- Mismatch →
415 Unsupported Media Type.
5.2. produces
- Defines what data format the API returns.
- Matches
client’s Accept header. - Example: produces = "application/xml" → returns XML response.
- Mismatch →
406 Not Acceptable.
@RequestMapping(
path = "/user",
method = RequestMethod.GET,
consumes = MediaType.APPLICATION_JSON_VALUE
)
@RequestMapping(
path = "/user",
method = RequestMethod.POST,
produces = MediaType.APPLICATION_JSON_VALUE
)
produces : defines response type (Accept header). consumes : defines request type (Content-Type header).
6. @RequestBody
Used to read the HTTP request body into a Java object (POJO).
- Matches with the consumes attribute.
- Incoming request body from a client.
- work as method parameter level specific to POJO class aligned to request body.
@PostMapping("/user")
public String createUser(@RequestBody User user) { ... }
// Here ,User class is a POJO class (not entity or any type)
7. @ResponceBody
- Outgoing responce body to a client.
- work as method level.
- Matches with the
produces attribute. - return type/ value converted to respective data format(JSON/XML) as per configuration produces attribute.
8. Content Negotiation & Conversion (produces/consumes)
Case 8.1 – Based on produces:
produces = "application/json" → JSON converter → JSON response
produces = "application/xml" → XML converter → XML response
Case 8.2 – Multiple Formats
Client Accept Header |
Converter Used | Response Format |
|---|---|---|
application/xml |
XML converter | XML response |
application/json |
JSON converter | JSON response |
*/* (any) |
JSON converter (default) | JSON response |
Spring checks the client’s Accept header and chooses the correct converter. Defaults to JSON if not specified.
Case 8.3 – Single Format
Client Accept Header |
Supported? | Response |
|---|---|---|
application/xml |
No | 406 Not Acceptable |
application/json |
Yes | JSON response |
*/* |
Yes | JSON response |
Only JSON responses are allowed.
Case 8.4 – Based on consumes:
Client Content-Type |
Supported? | Action |
|---|---|---|
| XML | Yes | Request accepted → XML converter |
| JSON | No | 415 Unsupported Media Type |
The consumes type specifies which input format the API accepts.