Request Mapping Annotation in Spring Boot

@RequestMapping is a class level (also called type level) and method level annotation, it is used to process HTTP requests with specified URL patterns. It is used in and along with both @Controller and @RestController.

1. How @RequestMapping annotation it is used?

@Controller
@RequestMapping("/student")
public class StudentController{

  @RequestMapping("/dashboard")
  public String dashboard(){
  return "dashboard";
  }

 @RequestMapping("result")
 public String result(){
 return "result";
 }
}

We can see in above code sample "/student" , "/dashboard" and "result" passed with annotation are called request value/path present in the URL patterns.

The class StudentController will handle all the requests like example.com/students

And the member functions(methods) inside the class will handle the requests followed by the example.com/students/**

    dashboard() method will handle the request coming to example.com/students/dashboard

    result() method will handle the request coming to example.com/students/result

2. Optional Elements of @RequestMapping

All fields are public and abstract

Modifier and Type Optional Element and Description
String[] consumes
Narrows the primary mapping by media types that can be consumed by the mapped handler.
String[]

headers

The headers of the mapped request, narrowing the primary mapping.
RequestMethod[] method
The HTTP request methods to map to, narrowing the primary mapping: GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE.
String name
Assign a name to this mapping.
String[] params
The parameters of the mapped request, narrowing the primary mapping.
String[] path
The path mapping URIs
String[] produces
Narrows the primary mapping by media types that can be produced by the mapped handler.
String[] value
The primary mapping expressed by this annotation.

 

2.1 name, value and path

These elements are quit confusing, seems they are same.

  • name - As in servlet mapping we assing name to the servlet class and url pattern and later it used in web.xml, in spring we can also assign name to the mapping. 
  • value & path - according to official documentation both are alias of each other, both accept string array (see in table) ,both works similar, we can conversely use any of them. But the question arises, if they are doing same work then why they exist?

2.2 headers, consumes and produces

  • headers - Headers are the meta data or extra message attached to HTTP requests. If we want to filter the request mappings on the basis of headers we need to specify that. It narrows down the mapping e.g. 
     @RequestMapping(value = "/something", headers = "content-type=text/*")
    it will accept all content types like "text/html", "text/plain", etc. Also we can see it contains expression "content-type=text" we can also negate that with != i.e "content-type!=text/" which means it will accept all "/something" having headers with content-type "application/json", "application/xml" , "image/png", "image/, etc except "text/". Here * is wild card used for all.

2.3 params

 

2.4 method

 

 

3. Specialization of @RequestMapping

@RequestMapping specialiszations are created on basis of HTTP request methods, the other elements provided in the above table are used similarly as per the requirement.

  1. @GetMapping
  2. @PutMapping
  3. @PostMapping
  4. @DeleteMapping
  5. @PatchMapping

3.1 @GetMapping

 

 



×