A simple comparison between Application/json and application/x-www-form-urlencoded

Source: Internet
Author: User

application/x-www-form-urlencoded Submission Request Example
curl -X POST ‘http://localhost:8080/formPost‘ -d ‘id=1&name=foo&mobile=13612345678‘
Wireshark Clutch Results

Corresponding service-side parsing parameters source code
//org.springframework.web.method.annotation.RequestParamMethodArgumentResolver#resolveNameif (arg == null) {   String[] paramValues = webRequest.getParameterValues(name); if (paramValues != null) { arg = paramValues.length == 1 ? paramValues[0] : paramValues; }}
Application/json Submission Request Example
curl -X POST -H "Content-Type: application/json" ‘http://localhost:8080/jsonPost‘ -d ‘{"id":2,"name":"foo","mobile":"13656635451"}‘
Wireshark Clutch Results

Corresponding service-side parsing parameters source code
Com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter#readInternalProtected Object readinternal (class<? extends Object> Clazz, Httpinputmessage inputmessage) throws IOException, httpmessagenotreadableexception {BytearrayoutputstreamBAOs = newBytearrayoutputstream (); InputStream in = Inputmessage.getbody ();Byte[]BUF = newbyte[1024]; while (true) {int bytes = in.read (buf); if (bytes = =-1) { byte[] bytes1 = Baos.tobytearray (); return Json.parseobject (bytes1,  0, Bytes1.length, This.charset.newDecoder (), Clazz, new feature[0]);} if (bytes > 0) { baos.write (buf, 0, bytes);      }}} 
Mixed-use examples

Web Layer Code

    @RequestMapping(value="/mixPost", method=RequestMethod.POST )    public Result<Void> mixPostTest(@RequestBody @Valid Foo foo, @RequestParam Integer sex)

Submit Request

curl -X POST -H "Content-Type: application/json" ‘http://localhost:8080/mixPost?sex=1‘ -d ‘{"id":2,"name":"foo","mobile":"13656635451"}‘
Add--How to locate the corresponding source code to find the POST request parsing parameters source code
    @RequestMapping(value="/formPost", method=RequestMethod.POST )    public Result<Void> formPostTest(@RequestParam int id, @RequestParam String name, @RequestParam String mobile)

Because the ID is a required parameter if the request parameter does not contain an ID, the error will be as follows

Org. springframework. Web. bind. missingservletrequestparameterexception:required int parameter' ID ' is not present at Org. springframework. Web. method. annotation. Requestparammethodargumentresolver. Handlemissingvalue (Requestparammethodargumentresolver. Java:255) at Org. springframework. Web. method. annotation. Abstractnamedvaluemethodargumentresolver. Resolveargument (Abstractnamedvaluemethodargumentresolver. Java:) at Org. springframework. Web. method. Support. Handlermethodargumentresolvercomposite. Resolveargument (Handlermethodargumentresolvercomposite. Java: +) at org. springframework. Web. Method. Invocablehandlermethod. Getmethodargumentvalues (Invocablehandlermethod. Java:157) at Org. Springframework. Web. Method. Invocablehandlermethod. Invokeforrequest (Invocablehandlermethod. Java:124)   

This method can be used to quickly locate the source code

Source code for JSON request parsing parameters found
    @RequestMapping(value="/jsonPost", method=RequestMethod.POST )    public Result<Void> jsonPostTest(@RequestBody @Valid Foo foo)

Because it is necessary to construct an empty Foo object before you can inject each property value so add a breakpoint in the non-parametric constructor of Foo, you can locate the source code of the JSON request parsing parameters

Reprinted: 1190000007252829

A simple comparison between Application/json and application/x-www-form-urlencoded

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.