1. @PathVariable
When using the @requestmapping URI template style mapping, which is Someurl/{paramid}, the Paramid can bind the value passed to the method by @Pathvariable annotations.
Example code:
- @Controller
- @RequestMapping ("/owners/{ownerid}")
- Public class Relativepathuritemplatecontroller {
- @RequestMapping ("/pets/{petid}")
- public void Findpet (@PathVariable string ownerid, @PathVariable string petid, model model) {
- //Implementation omitted
- }
- }
3, @RequestParam, @RequestBody
@RequestParam
A) commonly used to deal with simple types of bindings, a String obtained by Request.getparameter () can be converted directly into a simple type case (string--> The conversion of a simple type is done by a converter configured by Conversionservice), because the parameter is obtained using the Request.getparameter () method, so you can handle the value of querystring in the Get mode, or you can handle the post mode The value of body data;
B) used to process Content-type: For application/x-www-form-urlencoded encoded content, submit way get, POST;
C) The note has two properties: value, required, value specifies the ID name to pass in the value, and required is used to indicate whether the parameter must be bound;
Example code:
- @Controller
- @RequestMapping ("/pets")
- @SessionAttributes ("pet")
- Public class Editpetform {
- // ...
- @RequestMapping (method = Requestmethod.get)
- Public String Setupform (@RequestParam ("Petid") int petid, Modelmap model) {
- Pet Pet = This.clinic.loadPet (petid);
- Model.addattribute ("Pet", pet);
- return "Petform";
- }
- // ...
@RequestBody
This annotation is often used to deal with Content-type: not application/x-www-form-urlencoded encoded content, such as Application/json, application/xml, etc.;
It is configured by using Handleradapter HttpMessageConverters to parse the post data body and then bind to the corresponding bean.
Because the configuration has formhttpmessageconverter, so can also be used to deal with application/x-www-form-urlencoded the content, processed results in a multivaluemap<string, string>, this situation in some special needs to use, See Formhttpmessageconverter API for details;
Example code:
- @RequestMapping (value = "/something", method = Requestmethod.put)
- Public void handle (@RequestBody String body, writer writer) throws IOException {
- Writer.write (body);
- }
Notes on @Pathvariable @RequestHeader of java-spring MVC's @requestparam @RequestBody