Spring's @RequestMapping annotation

@RequestMapping

RequestMapping is an annotation for processing request address mapping, which can be used on classes or methods. Used on a class, indicating that all methods in the class that respond to requests use this address as the parent path.

The RequestMapping annotation has six attributes. Let's divide it into three categories for description.

1、 value, method;

value: Specify the actual address of the request, the specified address can be in URI Template mode (will be explained later);

method: Specify the method type of the request, GET, POST, PUT, DELETE, etc.;

 

2、 consumes,produces;

consumes: Specify the submitted content type (Content-Type) for processing requests, such as application/json, text/html;

produces: specifies the content type to be returned, which is only returned when the (Accept) type in the request header contains the specified type;

 

3、 params,headers;

params: Specifies that the request must contain certain parameter values ​​for this method to process.

headers: The specified request must contain certain specified header values ​​in order for this method to process the request. 

  1. @RequestMapping(value="/owners/{ownerId}", method=RequestMethod.GET)
  2. public String findName(@PathVariable(name) String name, Model model) {
  3. User user = userService.findUser(name);
  4. model.addAttribute("user ", user );
  5. return "user";
  6. }  

 Use: params

 params= "myParam=myValue"   only processes requests that contain the name "myParam" and the value is "myValue"; 

@RequestMapping(value = "/pets/{petId}", method = RequestMethod.GET, params="myParam=myValue")

public void findPet ( athPathVariable String ownerId, athPathVariable String petId, Model model)

 

use: headers 

headers= "Referer=http://www.baidu.com/"   Only the header of the request contains the specified "Refer" request header and http://www.baidu.com/the corresponding value of " ";

RequestMapping(value = "/pets", method = RequestMethod.GET, headers="Referer=http://www.baidu.com/")  

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326550474&siteId=291194637