Address mapping of @RequestMapping usage details (transfer)

The article comes from: http://blog.sina.com.cn/s/blog_72827fb10101pl9i.html

Introduction:
       Some time ago, the RESTful mode was used in the project to develop the program, but when the data was submitted in the POST and PUT modes, it was found that the server side could not accept To the submitted data (the server-side parameter binding does not add any annotations), it is checked that the submission method is application/json, and the data printed by the server through request.getReader() does exist in the data submitted by the browser. In order to find out the reason, I researched the parameter binding (@RequestParam, @RequestBody, @RequestHeader, @PathVariable), and also looked at the relevant content of HttpMessageConverter, which is summarized here.

Introduction:
@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) of the processing request, such as application/json, text/html;
produces: specifies the type of content 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, so that the method is allowed deal with.
headers: The specified request must contain certain specified header values ​​in order for this method to process the request.

Example:
1. Example of value / method
Default RequestMapping("....str...") is the value of value;

@Controller  
@RequestMapping("/appointments")  
public class AppointmentsController {  
  
    private AppointmentBook appointmentBook;  
      
    @Autowired  
    public AppointmentsController(AppointmentBook appointmentBook) {  
        this.appointmentBook = appointmentBook;  
    }  
  
    @RequestMapping(method = RequestMethod.GET)  
    public Map<String, Appointment> get() {  
        return appointmentBook.getAppointmentsForToday();  
    }  
  
    @RequestMapping(value="/{day}", method = RequestMethod.GET)  
    public Map<String, Appointment> getForDay(@PathVariable @DateTimeFormat(iso=ISO.DATE) Date day, Model model) {  
        return appointmentBook.getAppointmentsForDay(day);  
    }  
  
    @RequestMapping(value="/new", method = RequestMethod.GET)  
    public AppointmentForm getNewForm() {  
        return new AppointmentForm();  
    }  
  
    @RequestMapping(method = RequestMethod.POST)  
    public String add(@Valid AppointmentForm appointment, BindingResult result) {  
        if (result.hasErrors()) {  
            return "appointments/new";  
        }  
        appointmentBook.addAppointment(appointment);  
        return "redirect:/appointments";  
    }  
}  



The uri value of value has the following three types:
A) It can be specified as a common specific value;
B) It can be specified as a type of value containing a variable (URI Template Patterns with Path Variables);
C) It can be specified as a regular expression A class of values ​​(URI Template Patterns with Regular Expressions);

example B)

@RequestMapping(value="/owners/{ownerId}", method=RequestMethod.GET)  
public String findOwner(@PathVariable String ownerId, Model model) {  
  Owner owner = ownerService.findOwner(ownerId);    
  model.addAttribute("owner", owner);    
  return "displayOwner";   
}  



example C)
@RequestMapping("/spring-web/{symbolicName:[a-z-]+}-{version:\d\.\d\.\d}.{extension:\.[a-z]}")  
  public void handle (@PathVariable String version, athPathVariable String extension)      
    // ...  
  }  
}
 

2 consumes, produces example example
of cosumes:
@Controller  
@RequestMapping(value = "/pets", method = RequestMethod.POST, consumes="application/json")  
public void addPet(@RequestBody Pet pet, Model model) {      
    // implementation omitted  
}  

The method only handles requests whose request Content-Type is "application/json".

Example of produces:
Java code collection code
@Controller  
@RequestMapping(value = "/pets/{petId}", method = RequestMethod.GET, produces="application/json")  
@ResponseBody  
public Pet getPet(@PathVariable String petId, Model model) {      
    // implementation omitted  
}   

The method only processes requests that contain "application/json" in the Accept header of the request request, and implies that the returned content type is application/json;

3 params, headers Example
of params:
@Controller  
@RequestMapping("/owners/{ownerId}")  
public class RelativePathUriTemplateController {  
  
  @RequestMapping(value = "/pets/{petId}", method = RequestMethod.GET, params="myParam=myValue")  
  public void findPet (@PathVariable String ownerId, athPathVariable String petId, Model model)      
    // implementation omitted  
  }  
}  

Only process requests that contain the name "myParam" and the value is "myValue";

examples of headers:
@Controller  
@RequestMapping("/owners/{ownerId}")  
public class RelativePathUriTemplateController {  
  
@RequestMapping(value = "/pets", method = RequestMethod.GET, headers="Referer=http://www.ifeng.com/")  
  public void findPet (@PathVariable String ownerId, athPathVariable String petId, Model model)      
    // implementation omitted  
  }  
}  

The header that only processes the request contains the specified "Refer" request header and the request with the corresponding value "http://www.ifeng.com/"; the

above only introduces which requests are processed by the method specified by RequestMapping. The following article will Explain how to process the data submitted by the request (data binding) and the returned data.

Guess you like

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