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

最新文章

  1. svg学习(五)ellipse
  2. &quot;微信全球商业创新大赛-创意中国2015&quot;国际MBA商业挑战赛开启
  3. Nopi .net下面的Excel第三方dll
  4. Android——TabWidget
  5. 【转载】OpenStack Swift学习笔记
  6. 【PythonChallenge】Level 4
  7. PMP考试的过与只是
  8. 关于GPS偏移的基础知识
  9. [SQL基础教程] 4-3 数据的更新(UPDATE)
  10. CollapsingToolbarLayout学习笔记
  11. 36.java_exception_test
  12. oracle-获取数据库中所有表的注释 comments
  13. RAID及热备盘详解
  14. Xamarin Layout属性(转)
  15. MyBatis学习总结(三)——多表关联查询与动态SQL
  16. Confluence 6 附件存储提取文本文件
  17. Collection中的方法
  18. DBeaver连接达梦数据库
  19. bzoj4566 / P3181 [HAOI2016]找相同字符
  20. three probing way of openadress hash

热门文章

  1. Java 编程下字符串的 16 位、32位 MD5 加密
  2. layui的table中使用switch
  3. Spring boot下添加filter
  4. html页面去掉滚动条
  5. Joint Extraction of Entities and Relations论文解析
  6. php连接oracle数据库
  7. docker探索-docker私有仓库搭建(九)
  8. regsvr32.exe是什么东西
  9. [转载]WPF控件拖动
  10. android:screenOrientation的说明