application/json 与 application/x-www-form-urlencoded的简单比较
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#resolveName
if (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#readInternal
protected 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请求解析参数的源码
转载:https://segmentfault.com/a/1190000007252829
application/json 与 application/x-www-form-urlencoded的简单比较的更多相关文章
- multipart/form-data,application/json和application/x-www-form-urlencoded区别
application/json和application/x-www-form-urlencoded都是表单数据发送时的编码类型. EncType: enctype 属性规定在发送到服务器之前应该如何 ...
- application/json和application/x-www-form-urlencoded区别
application/json和application/x-www-form-urlencoded都是表单数据发送时的编码类型. EncType: enctype 属性规定在发送到服务器之前应该如何 ...
- 在同一个项目中灵活运用application/json 和application/x-www-form-urlencoded 两种传输格式(配合axios,同时配置loading)
'use strict' import axios from 'axios' // import qs from 'qs' import { Notification} from 'element-u ...
- vue(axios)封装,content-type由application/json转换为application/x-www-form-urlencoded
现在主流的http请求头的content-type有三种(不讨论xml): application/x-www-form-urlencoded 最常见的提交数据方式,与原生form表单数据一致,在c ...
- application/json和application/x-www-form-urlencoded使用选择
一.参考资料 选application/x-www-form-urlencoded还是application/json? @RequestBody应用 二.理解 1.@RequestBody的作用 注 ...
- application/json 和 application/x-www-form-urlencoded的区别
public static string HttpPost(string url, string body) { //ServicePointManager.ServerCertificateVali ...
- application/json和application/x-www-form-urlencoded参数接收
application/json ajax请求中content-type:application/json代表参数以json字符串传递给后台,controller接收需要@RequestBody 接收 ...
- IE10以下的IE浏览器在form表单提交、a标签等场景下,接收application/json类型的响应时,会提示是否要下载该json文件
IE10以下的IE浏览器并不支持application/json这种response格式,所以需要在服务端先将对象转成json字符串然后,设置Content-Type为text/html的类型,再返回 ...
- !!!四种常见的 POST 提交数据方式(含application/json)
HTTP/1.1 协议规定的 HTTP 请求方法有 OPTIONS.GET.HEAD.POST.PUT.DELETE.TRACE.CONNECT 这几种.其中 POST 一般用来向服务端提交数据,本文 ...
随机推荐
- 读取本地已有的.db数据库
public class MyDB extends SQLiteOpenHelper { // 数据库的缺省路径 private static String DB_PATH ; private sta ...
- Java后端测试概述
[本文出自天外归云的博客园] 多种单测技术 1. 要学会Spring MVC/Boot测试中自带的mock方法. 2. 学会junit中的方法,对于注解的使用等. 3. 学会使用结合第三方Mockit ...
- js之获取url中"?"后面的字串
url : index.php?id=123 <script type="text/javascript"> function GetRequest() { var u ...
- ORA-01403:no data found 解决办法
原因:select a into b from table:当查询出来的a没有数据时,这个时候就会抛出这个异常:ORA-01403:no data found 解决方法: 先定义一个整形变量,coun ...
- [转]MySQL中int(11)最大长度是多少?
原文地址:https://blog.csdn.net/allenjay11/article/details/76549503 今天在添加数据的时候,发现当数据类型为 int(11) 时,我当时让用户添 ...
- 【Unity笔记】根骨骼动画/运动(Root Motion)
根骨骼动画:当动画中角色发生位移后,动作坐标原点跟随角色移动.例如一个向前跳跃的动画,如果在场景中重复该动画,能够看到角色一路往前跳跃,位置一直在前进.适用于有位移的放技能动作等. 非根骨骼动画:当动 ...
- 【WPF/C#】图层筛选/拾取——Color Picker
文章标题实在不懂怎么表述才清楚. 描述问题:多个图片(图层)重叠时,如何通过鼠标点击来拾取到相应的图层.因为图层中会有很多空白的地方(比如图片四周),要求是获取鼠标点击位置的像素颜色值,如果为空白时或 ...
- Postgres创建管理员角色
--创建角色,赋予角色属性 ' superuser createrole createdb --添加到角色组 grant postgres to batman 以上是直接创建管理员角色,如果是修改一个 ...
- bluetooth在linux应用开发
linux内Bluetooth的协议栈为BlueZ,http://www.bluez.org/.在4.46上,BlueZ实现了对A2DP Sink的支持,而之前的版本只支持A2DP Source.
- docker:Error running DeviceCreate (createSnapDevice) dm_task_run failed
1) service docker stop 2) thin_check /home/docker/devicemapper/devicemapper/metadata 3) thin_check - ...