application/json 与 application/x-www-form-urlencoded的简单比较

来源:互联网 发布:邵子神数软件 编辑:程序博客网 时间:2024/06/15 02:32

www-form-urlencoded是POST数据默认编码格式,POST过去的key-value会被编码成QueryString,格式如下:

name=test&gender=male&email=iefreer@live.cn


服务器端对接受数据的处理也很简单。


json一般更多用来返回数据而不是在提交数据的时候使用,通常Restful服务都会支持json/xml格式的返回数据。

json对象及其编解码在各个平台(无论服务器、电脑还是手机)都有很好的支持。


那么怎么选择这两种方式呢?这不是一个非错即对的选择,但可以参考如下的实践建议:

如果数据是简单、平面的key-value数值对,那么使用www-form-urlencoded简单实用,不需要额外的编解码;

如果数据是复杂的嵌套关系,有多层数据,那么使用json会简化数据的处理,从而更高效。


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);        }    }}

混用示例 (PS:本人没有测试成功)

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请求解析参数的源码

原创粉丝点击