application/json 與 application/x-www-form-urlencoded的簡單比較

來源:互聯網
上載者:User

標籤:sci   分享圖片   一個   out   urlencode   resolve   hand   ade   cat   

application/x-www-form-urlencoded提交請求樣本
curl -X POST ‘http://localhost:8080/formPost‘ -d ‘id=1&name=foo&mobile=13612345678‘
wireshark抓包結果

對應的服務端解析參數源碼
//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提交請求樣本
curl -X POST -H "Content-Type: application/json" ‘http://localhost:8080/jsonPost‘ -d ‘{"id":2,"name":"foo","mobile":"13656635451"}‘
wireshark抓包結果

對應的服務端解析參數源碼
//com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter#readInternalprotected Object readInternal(Class<? extends Object> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {    ByteArrayOutputStream baos = new ByteArrayOutputStream();    InputStream in = inputMessage.getBody();    byte[] buf = new byte[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);        }    }}
混用樣本

web層代碼

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

提交請求

curl -X POST -H "Content-Type: application/json" ‘http://localhost:8080/mixPost?sex=1‘ -d ‘{"id":2,"name":"foo","mobile":"13656635451"}‘
補充--如何定位對應的源碼找到post請求解析參數源碼
    @RequestMapping(value="/formPost", method=RequestMethod.POST )    public Result<Void> formPostTest(@RequestParam int id, @RequestParam String name, @RequestParam String mobile)

因為id是必填參數 如果請求參數中不含id的話 會報錯 如下所示

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:95)    at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:79)    at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:157)    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:124)

通過此方法可以快速定位到源碼

找到json請求解析參數的源碼
    @RequestMapping(value="/jsonPost", method=RequestMethod.POST )    public Result<Void> jsonPostTest(@RequestBody @Valid Foo foo)

因為肯定要先構造一個空Foo對象 然後才能注入各屬性值 所以在Foo的無參建構函式中加斷點, 可以定位到json請求解析參數的源碼

轉載:1190000007252829

application/json 與 application/x-www-form-urlencoded的簡單比較

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.