SpringMVC request parameter annotation two small problems

I encountered two small problems using SpringMVC request annotations today:

If it is annotated with @requestBody, the content type of the request body should generally be application/json. If its type is multipart/form-data, an error will be reported: Unsupported media type;

If annotated with @requestParam, the parameter corresponding to the parameter name must be passed by default, otherwise an error will be reported;

@RequestParam

Used to handle Content-Type: content encoded for application/x-www-form-urlencoded. (In the Http protocol, if Content-Type is not specified, the default passed parameter is application/x-www-form-urlencoded type)

RequestParam can accept properties of simple types as well as object types. 
The essence is to configure the Key-Value parameter Map in Request.getParameter() using Spring's conversion mechanism, ConversionService, and convert it into a parameter receiving object or field.

tip

In Content-Type: application/x-www-form-urlencodedthe request, 
the value of queryString in the get method and the value of the body data in the post method will be accepted by the Servlet and converted into the Request.getParameter() parameter set, so @RequestParam can get it.

@RequestBody

To process the data passed by HttpEntity, it is generally used to process data in non- Content-Type: application/x-www-form-urlencodedencoded format.

  • In GET requests, because there is no HttpEntity, @RequestBody does not apply.
  • In the POST request, the parameters passed through HttpEntity must declare the data type in the request header Content-Type. SpringMVC parses the data in HttpEntity by using HttpMessageConverters configured by HandlerAdapter, and then binds it to the corresponding bean.

Summarize

  • In a GET request, @RequestBody cannot be used.
  • In POST requests, @RequestBody and @RequestParam can be used, but if @RequestBody is used, the configuration for parameter conversion must be unified.

For example, in the HttpMessageConverters processing stack configured in SpringMVC, specify the format of json conversion, such as Date is converted to 'yyyy-MM-dd', then if the field contained in the parameter receiving object is of Date type, it can only be passed by the client. The format of year, month and day cannot pass hours, minutes and seconds. Because of different interfaces, its parameters may have different format requirements for time parameters, so doing this will make the client call colleagues a little confused about the format of the parameters, so the scalability is not high.

If you use @RequestParam to accept parameters, you can set @DateFormat in the model that accepts parameters to specify the format that needs to accept time parameters.

In addition, the parameters accepted by @RequestBody will not be converted by Servlet and placed in the Param parameter set of the request object. @RequestParam is fine.

To sum up, in general, it is recommended to use the @RequestParam annotation to accept Http request parameters.

 https://blog.csdn.net/xinluke/article/details/52710706

Guess you like

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