Spring @RequestMapping Annotation Examples

@RequestMapping

This annotation is for mapping web requests onto methods in request-handling classes with flexible method signatures. Spring MVC and Spring WebFlux, both support this annotation through a RequestMappingHandlerMapping and RequestMappingHandlerAdapter in their respective modules and package structure.

For the exact list of supported handler method arguments and return types in each, please use the reference documentation links below:

Spring MVC Method Arguments and Return Values Spring WebFlux Method Arguments and Return Values Note: This annotation can be used both at the class and at the method level. In most cases, at the method level applications will prefer to use one of the HTTP method-specific variants @GetMapping, @PostMapping, @PutMapping, @DeleteMapping, or @PatchMapping.

To configure the mapping of web requests, you use the @RequestMapping annotation.
The @RequestMapping annotation can be applied to class-level and/or method-level in a controller.

The class-level annotation maps a specific request path or pattern onto a controller. You can then apply additional method-level annotations to make mappings more specific to handler methods.

1). @RequestMapping annotations at the method level

In this approach, we have to give complete paths as value attributes.

@Controller public class StudentController {@RequestMapping("/api/v1/student") public String getAllStudents(Model model) { // code return studentList; } @RequestMapping("/api/v1/student/add") public String addEmployee(Student student) { // code return student; } @RequestMapping("/api/v1/student/update") public String updateStudent(Student student) { // code return student; } @RequestMapping(value = { "/api/v1/student/delete" }) public String removeStudent(@RequestParam("id") String id) { // code return studentList; } }

2). @RequestMapping annotations at class and method level

@Controller @RequestMapping("/api/v1/student/*") public class StudentController { public String getAllStudents(Model model) { // code return studentList; } @RequestMapping("/add") public String addStudent(Student student) { // code return student; } @RequestMapping("/update") public String updateStudent(Student student) { // code return student; } @RequestMapping(value = { "/delete" }) public String removeStudent(@RequestParam("id") String id) { // code return studentList; } }

3). @RequestMapping annotations using only HTTP request types

We can have only one @RequestMapping annotation at the class level and at method levels, we need not specify any value. We have to specify the HTTP request types so that each different Http type is mapped to a different method.

@Controller @RequestMapping("/api/v1/student/*") public class StudentController { @RequestMapping (method = RequestMethod.GET) public String getAllStudents(Model model) { // code return studentList; } @RequestMapping (method = RequestMethod.POST) public String addStudent(Student student) { // code return student; } @RequestMapping (method = RequestMethod.PUT) public String updateStudent(Student student) { // code return student; } @RequestMapping (method = RequestMethod.DELETE) public String removeStudent(@RequestParam("id") String id) { // code return studentList; } }

4). @RequestMapping with Produces and Consumes

We can use the header Content-Type and Accept to find out request contents and what is the mime message it wants in response. For clarity, @RequestMapping provides produces and consumes variables where we can specify the request content-type for which method will be invoked and the response content type. e.g

@RequestMapping(value="/add", produces={"application/json","application/xml"}, consumes="text/html") @ResponseBody public String addStudent(Student student){ //code return student; }

4). @RequestMapping With @RequestParam

The @RequestParam annotation is used with @RequestMapping to bind a web request parameter to the parameter of the handler method.

The @RequestParam annotation can be used with or without a value. The value specifies the request param that needs to be mapped to the handler method parameter.

@RequestMapping(value="/id") String getById(@RequestParam String id) { //code return student; }

In this article, we have seen Spring @RequestMapping Annotation Examples.