@RequestMapping is one of the most widely used Spring MVC annotation. org.springframework.web.bind.annotation.RequestMapping annotation is used to map web requests onto specific handler classes and/or handler methods.
@RequestMapping with Multiple URI: We can use a single method for handling multiple URIs, for example:
@RequestMapping(value={"/method1","/method1/second"})
@ResponseBody
public String method1(){
return "method1";
}
@RequestMapping with HTTP Method: Sometimes we want to perform different operations based on the HTTP method used, even though request URI remains same. We can use @RequestMapping method variable to narrow down the HTTP methods for which this method will be invoked. For example:
@RequestMapping(value="/method2", method=RequestMethod.POST)
@ResponseBody
public String method2(){
return "method2";
}
@RequestMapping(value="/method3", method={RequestMethod.POST,RequestMethod.GET})
@ResponseBody
public String method3(){
return "method3";
}
@RequestMapping with Headers: We can specify the headers that should be present to invoke the handler method. For example:
@RequestMapping(value="/method4", headers="name=pankaj")
@ResponseBody
public String method4(){
return "method4";
}
@RequestMapping(value="/method5", headers={"name=pankaj", "id=1"})
@ResponseBody
public String method5(){
return "method5";
}
@RequestMapping with Produces and Consumes: We can use 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. For example:
@RequestMapping(value="/method6", produces={"application/json","application/xml"}, consumes="text/html")
@ResponseBody
public String method6(){
return "method6";
}
Above method can consume message only with Content-Type as text/html and is able to produce messages of type application/json and application/xml.
@RequestMapping with @PathVariable: RequestMapping annotation can be used to handle dynamic URIs where one or more of the URI value works as a parameter. We can even specify Regular Expression for URI dynamic parameter to accept only specific type of input. It works with @PathVariable annotation through which we can map the URI variable to one of the method arguments. For example:
@RequestMapping(value="/method7/{id}")
@ResponseBody
public String method7(@PathVariable("id") int id){
return "method7 with id="+id;
}
@RequestMapping(value="/method8/{id:[\\d]+}/{name}")
@ResponseBody
public String method8(@PathVariable("id") long id, @PathVariable("name") String name){
return "method8 with id= "+id+" and name="+name;
}
@RequestMapping with @RequestParam for URL parameters: Sometimes we get parameters in the request URL, mostly in GET requests. We can use @RequestMapping with @RequestParam annotation to retrieve the URL parameter and map it to the method argument. For example:
@RequestMapping(value="/method9")
@ResponseBody
public String method9(@RequestParam("id") int id){
return "method9 with id= "+id;
}
For this method to work, the parameter name should be “id” and it should be of type int.
@RequestMapping default method: If value is empty for a method, it works as default method for the controller class. For example:
@RequestMapping()
@ResponseBody
public String defaultMethod(){
return "default method";
}
As you have seen above that we have mapped /home to HomeController, this method will be used for the default URI requests.
@RequestMapping fallback method: We can create a fallback method for the controller class to make sure we are catching all the client requests even though there are no matching handler methods. It is useful in sending custom 404 response pages to users when there are no handler methods for the request.
@RequestMapping("*")
@ResponseBody
public String fallbackMethod(){
return "fallback method";
}
-----------------------------------------------------------------------------------------------------------------------
Example :
package com.journaldev.spring;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* Handles requests for the application home page.
*/
@Controller
@RequestMapping("/home")
public class HomeController {
curl http://localhost:9090/SpringRequestMappingExample/home/method0
method0
@RequestMapping(value="/method0")
@ResponseBody
public String method0(){
return "method0";
}
curl http://localhost:9090/SpringRequestMappingExample/home/method1
method1
curl http://localhost:9090/SpringRequestMappingExample/home/method1/second
method1
@RequestMapping(value={"/method1","/method1/second"})
@ResponseBody
public String method1(){
return "method1";
}
curl -X POST http://localhost:9090/SpringRequestMappingExample/home/method2
method2
@RequestMapping(value="/method2", method=RequestMethod.POST)
@ResponseBody
public String method2(){
return "method2";
}
curl -X POST http://localhost:9090/SpringRequestMappingExample/home/method3
method3
curl -X GET http://localhost:9090/SpringRequestMappingExample/home/method3
method3
@RequestMapping(value="/method3", method={RequestMethod.POST,RequestMethod.GET})
@ResponseBody
public String method3(){
return "method3";
}
curl -H name:pankaj http://localhost:9090/SpringRequestMappingExample/home/method4
method4
@RequestMapping(value="/method4", headers="name=pankaj")
@ResponseBody
public String method4(){
return "method4";
}
curl -H name:pankaj -H id:1 http://localhost:9090/SpringRequestMappingExample/home/method5
method5
@RequestMapping(value="/method5", headers={"name=pankaj", "id=1"})
@ResponseBody
public String method5(){
return "method5";
}
curl -H Content-Type:text/html http://localhost:9090/SpringRequestMappingExample/home/method6
method6
curl -H Content-Type:text/html -H Accept:application/json -i http://localhost:9090/SpringRequestMappingExample/home/method6
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: application/json
Content-Length: 7
Date: Thu, 03 Jul 2014 18:14:10 GMT
curl -H Content-Type:text/html -H Accept:application/xml -i http://localhost:9090/SpringRequestMappingExample/home/method6
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: application/xml
Content-Length: 7
Date: Thu, 03 Jul 2014 18:14:10 GMT
method6
@RequestMapping(value="/method6", produces={"application/json","application/xml"}, consumes="text/html")
@ResponseBody
public String method6(){
return "method6";
}
curl http://localhost:9090/SpringRequestMappingExample/home/method7/1
method7 with id=1
@RequestMapping(value="/method7/{id}")
@ResponseBody
public String method7(@PathVariable("id") int id){
return "method7 with id="+id;
}
curl http://localhost:9090/SpringRequestMappingExample/home/method8/10/Lisa
method8 with id= 10 and name=Lisa
@RequestMapping(value="/method8/{id:[\\d]+}/{name}")
@ResponseBody
public String method8(@PathVariable("id") long id, @PathVariable("name") String name){
return "method8 with id= "+id+" and name="+name;
}
curl http://localhost:9090/SpringRequestMappingExample/home/method9?id=20
method9 with id= 20
@RequestMapping(value="/method9")
@ResponseBody
public String method9(@RequestParam("id") int id){
return "method9 with id= "+id;
}
curl http://localhost:9090/SpringRequestMappingExample/home
default method
@RequestMapping()
@ResponseBody
public String defaultMethod(){
return "default method";
}
curl http://localhost:9090/SpringRequestMappingExample/home/xyz
fallback method
curl http://localhost:9090/SpringRequestMappingExample/home/method6
fallback method
@RequestMapping("*")
@ResponseBody
public String fallbackMethod(){
return "fallback method";
}
}
@RequestMapping with Multiple URI: We can use a single method for handling multiple URIs, for example:
@RequestMapping(value={"/method1","/method1/second"})
@ResponseBody
public String method1(){
return "method1";
}
@RequestMapping with HTTP Method: Sometimes we want to perform different operations based on the HTTP method used, even though request URI remains same. We can use @RequestMapping method variable to narrow down the HTTP methods for which this method will be invoked. For example:
@RequestMapping(value="/method2", method=RequestMethod.POST)
@ResponseBody
public String method2(){
return "method2";
}
@RequestMapping(value="/method3", method={RequestMethod.POST,RequestMethod.GET})
@ResponseBody
public String method3(){
return "method3";
}
@RequestMapping with Headers: We can specify the headers that should be present to invoke the handler method. For example:
@RequestMapping(value="/method4", headers="name=pankaj")
@ResponseBody
public String method4(){
return "method4";
}
@RequestMapping(value="/method5", headers={"name=pankaj", "id=1"})
@ResponseBody
public String method5(){
return "method5";
}
@RequestMapping with Produces and Consumes: We can use 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. For example:
@RequestMapping(value="/method6", produces={"application/json","application/xml"}, consumes="text/html")
@ResponseBody
public String method6(){
return "method6";
}
Above method can consume message only with Content-Type as text/html and is able to produce messages of type application/json and application/xml.
@RequestMapping with @PathVariable: RequestMapping annotation can be used to handle dynamic URIs where one or more of the URI value works as a parameter. We can even specify Regular Expression for URI dynamic parameter to accept only specific type of input. It works with @PathVariable annotation through which we can map the URI variable to one of the method arguments. For example:
@RequestMapping(value="/method7/{id}")
@ResponseBody
public String method7(@PathVariable("id") int id){
return "method7 with id="+id;
}
@RequestMapping(value="/method8/{id:[\\d]+}/{name}")
@ResponseBody
public String method8(@PathVariable("id") long id, @PathVariable("name") String name){
return "method8 with id= "+id+" and name="+name;
}
@RequestMapping with @RequestParam for URL parameters: Sometimes we get parameters in the request URL, mostly in GET requests. We can use @RequestMapping with @RequestParam annotation to retrieve the URL parameter and map it to the method argument. For example:
@RequestMapping(value="/method9")
@ResponseBody
public String method9(@RequestParam("id") int id){
return "method9 with id= "+id;
}
For this method to work, the parameter name should be “id” and it should be of type int.
@RequestMapping default method: If value is empty for a method, it works as default method for the controller class. For example:
@RequestMapping()
@ResponseBody
public String defaultMethod(){
return "default method";
}
As you have seen above that we have mapped /home to HomeController, this method will be used for the default URI requests.
@RequestMapping fallback method: We can create a fallback method for the controller class to make sure we are catching all the client requests even though there are no matching handler methods. It is useful in sending custom 404 response pages to users when there are no handler methods for the request.
@RequestMapping("*")
@ResponseBody
public String fallbackMethod(){
return "fallback method";
}
-----------------------------------------------------------------------------------------------------------------------
Example :
package com.journaldev.spring;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* Handles requests for the application home page.
*/
@Controller
@RequestMapping("/home")
public class HomeController {
curl http://localhost:9090/SpringRequestMappingExample/home/method0
method0
@RequestMapping(value="/method0")
@ResponseBody
public String method0(){
return "method0";
}
curl http://localhost:9090/SpringRequestMappingExample/home/method1
method1
curl http://localhost:9090/SpringRequestMappingExample/home/method1/second
method1
@RequestMapping(value={"/method1","/method1/second"})
@ResponseBody
public String method1(){
return "method1";
}
curl -X POST http://localhost:9090/SpringRequestMappingExample/home/method2
method2
@RequestMapping(value="/method2", method=RequestMethod.POST)
@ResponseBody
public String method2(){
return "method2";
}
curl -X POST http://localhost:9090/SpringRequestMappingExample/home/method3
method3
curl -X GET http://localhost:9090/SpringRequestMappingExample/home/method3
method3
@RequestMapping(value="/method3", method={RequestMethod.POST,RequestMethod.GET})
@ResponseBody
public String method3(){
return "method3";
}
curl -H name:pankaj http://localhost:9090/SpringRequestMappingExample/home/method4
method4
@RequestMapping(value="/method4", headers="name=pankaj")
@ResponseBody
public String method4(){
return "method4";
}
curl -H name:pankaj -H id:1 http://localhost:9090/SpringRequestMappingExample/home/method5
method5
@RequestMapping(value="/method5", headers={"name=pankaj", "id=1"})
@ResponseBody
public String method5(){
return "method5";
}
curl -H Content-Type:text/html http://localhost:9090/SpringRequestMappingExample/home/method6
method6
curl -H Content-Type:text/html -H Accept:application/json -i http://localhost:9090/SpringRequestMappingExample/home/method6
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: application/json
Content-Length: 7
Date: Thu, 03 Jul 2014 18:14:10 GMT
curl -H Content-Type:text/html -H Accept:application/xml -i http://localhost:9090/SpringRequestMappingExample/home/method6
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: application/xml
Content-Length: 7
Date: Thu, 03 Jul 2014 18:14:10 GMT
method6
@RequestMapping(value="/method6", produces={"application/json","application/xml"}, consumes="text/html")
@ResponseBody
public String method6(){
return "method6";
}
curl http://localhost:9090/SpringRequestMappingExample/home/method7/1
method7 with id=1
@RequestMapping(value="/method7/{id}")
@ResponseBody
public String method7(@PathVariable("id") int id){
return "method7 with id="+id;
}
curl http://localhost:9090/SpringRequestMappingExample/home/method8/10/Lisa
method8 with id= 10 and name=Lisa
@RequestMapping(value="/method8/{id:[\\d]+}/{name}")
@ResponseBody
public String method8(@PathVariable("id") long id, @PathVariable("name") String name){
return "method8 with id= "+id+" and name="+name;
}
curl http://localhost:9090/SpringRequestMappingExample/home/method9?id=20
method9 with id= 20
@RequestMapping(value="/method9")
@ResponseBody
public String method9(@RequestParam("id") int id){
return "method9 with id= "+id;
}
curl http://localhost:9090/SpringRequestMappingExample/home
default method
@RequestMapping()
@ResponseBody
public String defaultMethod(){
return "default method";
}
curl http://localhost:9090/SpringRequestMappingExample/home/xyz
fallback method
curl http://localhost:9090/SpringRequestMappingExample/home/method6
fallback method
@RequestMapping("*")
@ResponseBody
public String fallbackMethod(){
return "fallback method";
}
}
Ref: https://www.journaldev.com/3358/spring-requestmapping-requestparam-pathvariable-example
No comments:
Post a Comment