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 一般用来向服务端提交数据,本文 ...
随机推荐
- centos6安装部署git服务器(gitlab6.4)
环境准备 python版本2.6git版本 1.8.4.1ruby版本ruby-2.0.0-p353gitlab-shell版本 v1.8.0gitlab版本6.4.3 因centos6系列的pyth ...
- 关于go1.6使用vendor的坑
基本命令是:$ govendor init$ govendor add +external 坑:如果在govendor之前,修改过go get的包源代码,那么必须在该包目录里git commit这个修 ...
- Beginning SDL 2.0(4) YUV加载及渲染
本文主要内容是基于的“Beginning SDL 2.0(3) SDL介绍及BMP渲染”(以下简称BS3)基础上,将BMP加载及渲染修改为YUV420或I420的原始视频格式.阅读完本部分内容相信你可 ...
- RestTemplate 发送 get 请求使用误区 多值为null
http://blog.csdn.net/zhousenshan/article/details/71055687 ****************************************** ...
- Windows 安装 setuptools 和 feedparser
一.安装setuptools: 页面: https://pypi.python.org/pypi/setuptools#downloads 1.下载该zip文件,解压,例如:C:\setuptools ...
- MATLAB 2016b 切换回英文版
原因: 中文下不能使用等间距字体.因为等间距字体都是英文字体,报错信息又是中文的,所以这时候报错就全是乱码.如果改成中文字体,又不是等间距的了,看着瞎眼. 方法: Preferences->Ge ...
- idea 实现热部署
Idea实现热部署需要以下几步 1.open module setting,设置path,使output path指向存放实时编译class的文件夹 2.设置Artifacts,选择exploded选 ...
- whatweb wordpress.rb
## # This file is part of WhatWeb and may be subject to # redistribution and commercial restrictions ...
- Phrase-Based & Neural Unsupervised Machine Translation基于短语非监督机器翻译
1. 前言 本文介绍一种无监督的机器翻译的模型.无监督机器翻译最早是<UNSUPERVISED NEURAL MACHINE TRANSLATION>提出.这个模型主要的特点,无需使用平行 ...
- 一文读懂BERT中的WordPiece
1. 前言 2018年最火的论文要属google的BERT,不过今天我们不介绍BERT的模型,而是要介绍BERT中的一个小模块WordPiece. 2. WordPiece原理 现在基本性能好一些的N ...