Annotation | Usage |
---|---|
@RequestMapping |
@RequestMapping(value = "/{name}", method = RequestMethod.GET, consumes="application/json" produces ="application/json", headers={"name=pankaj", "id=1"}) |
path (or) (or) name (or) and value: which URL the method is mapped to method: compatible HTTP methods params: filters requests based on presence, absence, or value of HTTP parameters headers: filters requests based on presence, absence, or value of HTTP headers consumes: which media types the method can consume in the HTTP request body produces: which media types the method can produce in the HTTP response body |
|
@RequestBody |
@RequestMapping(method = RequestMethod.POST) @ResponseBody public HttpStatus something(@RequestBody MyModel myModel) { return HttpStatus.OK; } |
with @RequestBody, Spring will bind the incoming HTTP request body(for the URL mentioned in @RequestMapping for that method) to that parameter. While doing that, Spring will [behind the scenes] use HTTP Message converters to convert the HTTP request body into domain object [deserialize request body to domain object], based on Accept header present in request. | |
@ResponseBody |
@RequestMapping(value = "/user/all", method = RequestMethod.GET) public @ResponseBody List<User> listAllUsers() { return userService.findAllUsers(); } |
with @ResponseBody, Spring will bind the return value to outgoing HTTP response body. While doing that, Spring will [behind the scenes] use HTTP Message converters to convert the return value to HTTP response body [serialize the object to response body], based on Content-Type present in request HTTP header | |
@RequestParam |
http://localhost:8080/springmvc/hello/101?param1=10¶m2=20 public String getDetails( @RequestParam(value="param1", required=true) String param1, @RequestParam(value="param2", required=false) String param2){ ... } |
@RequestParam is to obtain an parameter from the URI as well.@RequestParam annotation used for accessing the query parameter values from the request defaultValue – This is the default value as a fallback mechanism if request is not having the value or it is empty. name – Name of the parameter to bind required – Whether the parameter is mandatory or not. If it is true, failing to send that parameter will fail. value – This is an alias for the name attribute |
|
@PathVariable |
'http://localhost:8080/springmvc/hello/101?param1=10¶m2=20 @RequestMapping("/hello/{id}") public String getDetails(@PathVariable(value="id") String id, @RequestParam(value="param1", required=true) String param1, @RequestParam(value="param2", required=false) String param2){ ....... } @GetMapping("/user/{firstName}/{lastName}") @ResponseBody public String handler(@MatrixVariable("firstName") String firstName, @MatrixVariable("lastName") String lastName ) { return "<br>Matxrix variable <br> " + "firstName =" + firstName +"<br>" + "lastName =" + lastName; } |
@PathVariable is to obtain some placeholder from the URI @MatrixVariable – a name-value pair within a path segment is referred as matrix variable. Matrix variables can appear in any path segment, each variable separated with a semicolon (;) and multiple values are separated by comma (,) i.e. http://www.example.com/employee/Mike;salary=45000;dept=HR http://www.example.com/car/Audi;color=RED,BLACK,WHITE |
|
@RequestHeader |
@Controller public class HelloController { @RequestMapping(value = "/hello.htm") public String hello( @RequestHeader(value="Accept") String accept, @RequestHeader(value="Accept-Language") String acceptLanguage, @RequestHeader(value="User-Agent", defaultValue="foo") String userAgent, HttpServletResponse response) { System.out.println("accept: " + accept); System.out.println("acceptLanguage: " + acceptLanguage); System.out.println("userAgent: " + userAgent); return null; } } |
Reading http requestheader is written in HelloController The advantage of using Spring @RequestHeader is that it will automatically throw an exception like HTTP Status 400 – Missing request header ‘X’ for method parameter of type, if the header is NOT sent in the input request (by setting required=true) @RequestHeader for facilitating use to get the header details easily in our controller class |