application/x-www-form-urlencoded与application/json

application/x-www-form-urlencodedapplication/json

 

http request scenario: front-end: SDK sends request / page Ajax submission via http . Background: Springboot service, Rest style, needs to intercept requests before the controller layer. 
 
 

Springboot supports Rest style, which brings great convenience to coding. @RequestBody allows us to directly request application/json and get the deserialized object at the controller layer. When there is a need for interception, this method does not Works again.

application/x-www-form-urlencoded is submitted through the form, in sevlet implementation, mutipart/form-data and application/x-www-form-urlencoded will be handled specially, request parameters will be placed in request.paramter , which is a map . We can get the parameters from the map for verification, or other interception requirements. The acquisition of the map is similar to the lazy loading of hibernate . When the request.getparamter( ) method is called, the servlet will read the request parameters from the request stream and load them into the map . InputStream will also store this data, but if this data is read, it will not be able to read the data when it reaches the controller layer. Similarly, when it reaches the controller layer after interception , the requested data has been loaded into the controller layer method parameters, and the actual The parameter object needs to have a set method, and the framework will call the attribute set in a reflective way.method injects data, data will only be injected into existing properties.

When transferring data with a content-type of application/json , the transferred object need only be json -serialized. When sending data as application/x-www-form-urlencoded . The content of the request needs to be submitted in the format of ..=..&..=.. , and the content in the request body will be split with " & " and " = ".

 

When we use content-type=application/json and use @RequestBody in the background , we can no longer get request data from request.paramter .

 

If the data is a simple, flat key-value pair, then using www-form-urlencoded is simple and practical, and does not require additional encoding and decoding;

If the data is a complex nested relationship with multiple layers of data, then using application/json will simplify the processing of the data

Choose application/x-www-form-urlencoded or application/json , depending on whether you have the need to obtain request data from request.paramter .

 


Guess you like

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