Spring MVC @RequestMapping Annotation Example
June 28, 2019
In Spring MVC @RequestMapping
plays the role of mapping URL to the method of controller class. To perform mapping there are different elements in @RequestMapping
annotation. These elements are value, method, headers, params , consumes, produces, name. The value element defines the URL. The narrow down of primary mapping can be achieved by the combination of two or more elements. To access query string, we can use @RequestParam
and to access path variable we can use @PathVariable
annotation as an argument. Here in this page, we will provide different scenarios to narrow the mapping of URL to methods using elements of @RequestMapping
.
value
Defines the URL of request. For the given value in @RequestMapping, the appropriate request is mapped.@RequestMapping at Class Level
@RequestMapping can be used at class level as well as method level. Find the code how to use at class level.
@Controller @RequestMapping("/app") public class HelloWorldController { @RequestMapping(value="hello") public String helloUser () { return "success"; } }
@RequestMapping("hello")
@RequestMapping only at Method Level
To annotate controller by @RequestMapping is optional. It can only annotate at method level.
@Controller public class HelloWorldController { @RequestMapping(value="hello") public String helloUser() { return "success"; } }
@RequestMapping with @RequestParam
To fetch query string from the URL, @RequestParam is used as an argument.
@Controller public class HelloWorldController { @RequestMapping(value="fetch") public String getInfo(@RequestParam("id") String id) { System.out.println("id:"+ id); return "success"; } }
@RequestMapping for Fallback
Using @RequestMapping, we can implement a fallback method. For every response file not found exception, this method will be called, in this way we can implement 404 response.
@RequestMapping(value="*") public String default() { return "success"; }
@RequestMapping with @PathVariable
To access path variable, spring provides @PathVariable that is used as an argument. We have to refer the variable in @RequestMapping using {}.
@RequestMapping(value="/fetch/{id}") public String getInfo(@PathVariable("id") String id) { System.out.println("id:"+ id); return "success"; }
method
To define a method to accept any specific request method, @RequestMapping has the element as method which is assigned by required request method.@RequestMapping(value="/fetch", method=RequestMethod.POST) public String getInfo() { return "success"; }
headers
@RequestMapping provides the headers element. We can specify our header to narrow down the mapping. The format for assigning the header is Header=Value.@RequestMapping(value="/fetch", headers={"content-type=text/plain", "id=100"}) public String getInfo() { return "success"; }
@RequestMapping(value="/fetch", headers={"content-type=text/*"})
params
We can narrow request mapping using params. I controller class, create more than one method with same @RequestMapping value but assign different params as required.@Controller public class HelloWorldController { @RequestMapping(value="/fetch", params = {"id=100"}) public String getInfo1(@RequestParam("id") String id) { System.out.println("Inside getInfo1"); return "success"; } @RequestMapping(value="/fetch", params = {"id=200"}) public String getInfo2(@RequestParam("id") String id) { System.out.println("Inside getInfo2"); return "success"; } }
consumes
@RequestMapping can narrow the URL mapping using consumes element with value. The media type is used with consumes.@RequestMapping(value="/fetch", consumes = {"text/plain", "application/XML"}) public String getInfo1() { return "success"; }
produces
Using produces element, we can narrow the mappings . Media type can be single or multiple.@RequestMapping(value="/fetch", produces = {"text/html", "application/JSON"}) public String getInfo1() { return "success"; }