Home  >  Spring MVC

Spring MVC @RequestMapping Annotation Example

By Arvind Rai, 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";
	}	
}  
To access the method, URL will be /app/hello. In @RequestMapping if we are using only value element, then value is necessary to mention. So we cal also write it as
@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";
	}	
}   
In this case the URL will be as /hello.

@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";
	}
}  
If we access the URL as http://localhost:8080/concretepage-1/fetch?id=key100 then the id will be accessed by @RequestParam and the output will be key100.

@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";
}  
If we access the URL as /fetch/200, then the path variable id we will be assigned the value 200.


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";
}  
@RequestMapping supports different methods like GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE.

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";
}  
If we write
@RequestMapping(value="/fetch", headers={"content-type=text/*"})  
It means the content type text/html and text/plain both can be accepted.

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";
	}
}   
When we access the URL /fetch?id=100 , method getInfo1() is called. When we access URL /fetch?id=200 , method getInfo2() is called and when we access /fetch?id=300 , HTTP Status 404 is received.

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";
}  
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us