In the development process of interaction with the front end, there have been several times the parameters can not be taken, took some time to troubleshoot the problem, the following simple summary.
Explanation of annotations
The front-end pass parameters that we're going to get are probably divided into the following four categories:
- Note for the Requet URI section: @PathVariable
- Note from the header section of the request: @RequestHeader, @CookieValue
- Note for the body part of the request: @RequestParam, @RequestBody
- The attribute type is annotation: @SessionAttributes, @ModelAttribute
@PathVariable
The annotation binds the value of the variable in the URI template to the parameter of the method. If the variable names in the method parameter names and URI template that need to be bound are inconsistent, you need to specify the name in the URI template in @pathvariable ("name").
code example:
The get mode, which uses @PathVariable binding input parameters, is ideal for restful styles. Because it hides the relationship between parameters and paths, it can improve the security of the website, static page, reduce the risk of malicious attack;
Post mode, there is no need to use this method, after all, parameters are exposed to the URL above.
@RequestHeader
Annotations can bind the value of the header portion of request requests to the parameters of the method, and if, @RequestHeader the binding parameters, the request header does not, it will be an error, such as a cookie.
@CookieValue
You can bind the value of the cookie in the request header to the parameters of the method. I have not actually used ~
@RequestParam
- A common use to handle simple types of bindings, a case where a String obtained by Request.getparameter () can be converted directly to a simple type (string--> the conversion operation of a simple type is done by a conversionservice configured converter) Because the parameter is obtained by using the Request.getparameter () method, the value of querystring in the Get mode can be handled, or the value of body data in the post mode can be processed;
- Processing Content-type: For
application/x-www-form-urlencoded encoded content, submit way get, POST, the two are no different;
- The note has two properties: value, required; value is used to specify the ID name to pass in the value, required is used to indicate whether the parameter must be bound;
@RequestBody
- Processing Content-type: Not
application/x-www-form-urlencoded encoded content, 如: application / js on ,application / xml and so on;
- The post data body is parsed by using the Handleradapter configuration
HttpMessageConverters and then bound 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;
Use time :
A) GET, post method, according to the request header Content-type value to determine:
- application/x-www-form-urlencoded, Optional (that is, not necessary, because the data of this situation @requestparam, @ModelAttribute can also be processed, of course @requestbody can also handle);
- Multipart/form-data, cannot be processed (i.e. using @requestbody cannot process data in this format);
- Other formats must be (other formats include Application/json, Application/xml, etc.). Data in these formats must be handled using @requestbody);
B) When put is submitted, it is judged according to the value of the request header Content-type:
- Application/x-www-form-urlencoded, must;
- Multipart/form-data, unable to deal with;
- Other formats, must;
Description: The data encoding format of the body part of request is specified by the Content-type of the header section;
@SessionAttributes (no use) @ModelAttribute problem modification: Front-end Ajax request, post mode, request header: Content-type:
application/jsonBackground access: This time the background can not get parameters, because the front-end is not submitted using form, parameter inside is no parameter values modified to: Use @requestbody annotations, the parameters to the object, directly to the object value of the front-end Ajax request, get way, to take the opportunity to use
| |
application/x-www-form-urlencoded |
application/json, application/xml |
multipart/ Form-data |
A single parameter |
object |
| @PathVariable |
GET, POST (doesn't make any sense) |
GET, POST (doesn't make any sense) |
GET, POST (doesn't make any sense) |
|
|
| @RequestHeader |
GET, POST |
GET, POST |
GET, POST |
|
|
| @CookieValue |
GET, POST |
GET, POST |
GET, POST |
|
|
| @RequestParam |
GET, POST |
—— |
—— |
√ |
—— |
| @RequestBody |
GET, POST |
GET, POST |
—— |
—— |
√ |
| @ModelAttribute |
GET, POST |
—— |
—— |
—— |
√ |
| @SessionAttributes |
|
|
|
|
|
Spring MVC gets annotations for the front-end parameters