@RequestBody and @ RequestParam, data binding and fastjson

Previously on:
Recently, when I was working on a project, the release suddenly changed the requirements. The original content type was application/json when the front-end submitted data, but now it is application/x-www-form-urlencoded.

I was overwhelmed by this wave of operation. I'm a little white. We haven't corrected it. What are these things. Later, under the "careful" instruction of the boss, I found out some ways.

Next, I will write a note according to my idea in case it will be changed in the future.

1, @ RequestBody and @ RequestParam

1.1 @RequestParam

  • @The parameters received by RequestParam are from the HTTP request body or the QueryString of the request url
  • Used to process content type: content encoded for application/x-www-form-urlencoded.
  • @RequestParam can accept properties of simple types or object types.
  • @RequestParam can also be used for GET, POST, DELETE and other types of requests.

1.2 @RequestBody

  • The data passed from HttpEntity is generally used to process data in non content type: application / x-www-form-urlencoded format
  • The annotation @ requestBody receives parameters from the requestBody, that is, the request body.
  • It is generally used to process data in non content type: application / x-www-form-urlencoded format, such as application/json, application/xml and other types of data (application/json is available, but form data and x-www-form-urlencoded are not available).
  • @RequestBody can be used for POST, DELETE and other types of requests.
(@RequestParam Map map)
application/json When, json The string part is not available, url Medium?You can add parameters later, form-data,x-www-form-urlencoded When available, but to Headers Inside Content-Type Delete
(@RequestParam String waterEleId,@RequestParam String enterpriseName)
application/json When, json The string part is not available, url Medium?Parameters added later are available
form-data,x-www-form-urlencoded When available, and the parameters can be out of order (i.e. the parameters passed from the front end or url The order of parameters in does not have to be the same as that in the background interface, as long as the field names are the same), but Headers Inside Content-Type Delete
(@RequestParam Object object)
No matter application/json,form-data,x-www-form-urlencoded Not available

It is neither @ RequestBody nor @ RequestParam, and the parameter receiving method is not specified

(Map map)
(Object object)
application/json Time: json The string part is not available, url Medium?Parameters added later are not available.
Because it is not specified, it doesn't know what to use json String part or?The parameter part is added later, so it can't be used at all
form-data,x-www-form-urlencoded Not available at
 
(HttpServletRequest request)
application/json Not available
form-data,x-www-form-urlencoded Available when

In the GET request, @ RequestBody is not applicable because there is no HttpEntity

2, Summary

stay GET Cannot use in request@RequestBody. 
stay POST Request, can use@RequestBody and@RequestParam,But if you use@RequestBody,The configuration of parameter conversion must be unified.

3, Data binding

On data binding, I read n blogs. There are a lot of online materials, so I won't go into detail.

For me, I simply understand the parameter passed in from the front end, whether in JSON, XML format or key value format of application/x-www-form-urlencoded table, and then the controller at the back end receives the parameter and binds it into String, Integer, List and Map sets and user-defined objects for operation

4, Use fastjason to convert the data submitted by the front-end POST application/x-www-form-urlencoded into user-defined objects

Because the JSON format is passed in from the front end of a property in a user-defined object, it can only be bound to a string during data binding. Therefore, it is necessary to convert it into JSON or user-defined object after entering parameters in the definition

To import dependent packages before using fastjson, it is recommended to use the latest version Click to enter the maven warehouse website of fastjson

<dependency>
     <groupId>com.alibaba</groupId>
     <artifactId>fastjson</artifactId>
     <version>1.2.75</version>
</dependency>

1. If the custom object is an array, the fastjson package is introduced to convert it into a json array

custom object jsonArray = JSONObject.parseArray(params.getItem(),custom object.class);

2. When the user-defined type is String, Map and other types or user-defined types, it can be used

T t = JSONObject.parseObject(data, T);

3. When calling other back-end services, the parameters passed in should conform to the json format. In this case, jsonStr can be used as the parameter

String jsonStr = JSONObject.toJSONString(data);

Keywords: Java JSON Maven

Added by njwan on Sun, 16 Jan 2022 10:43:08 +0200