SpringMVC @RequestBody的使用
@RequestBody的作用
@RequestBody用于读取Request请求的body数据,然后利用SpringMVC配置的HttpMessageConverter对数据进行转换,最后把转换后的数据绑定到被@RequestBody注解的参数上;
@RequestBody的使用场景
根据request header中 Content-Type和被@RequestBody注解的参数不同,最常见的应用场景如下:
- Content-Type为application/json
- 参数为JavaBean:可实现json反序列化为JavaBean,使用的HttpMessageConverter为 MappingJackson2HttpMessageConverter
- 参数为String:简单将字符串赋值给参数,使用的HttpMessageConverter为 StringHttpMessageConverter
- Content-Type为application/xml
- 参数为JavaBean:可实现xml反序列化为JavaBean,使用的HttpMessageConverter为 Jaxb2RootElementHttpMessageConverter
- 参数为String:简单将字符串赋值给参数,使用的HttpMessageConverter为 StringHttpMessageConverter
- application/x-www-form-urlencoded
- 参数为String:简单将字符串赋值给参数,使用的HttpMessageConverter为 StringHttpMessageConverter
HttpMessageConverter接口
该接口定义了五个方法,分别是读取数据时的 canRead()、read() ,写入数据时的canWrite()、 write()方法以及获取支持类型的 getSupportedMediaTypes()
public interface HttpMessageConverter<T> {
// Indicate whether the given class and media type can be read by this converter.
boolean canRead(Class<?> clazz, MediaType mediaType);
// Indicate whether the given class and media type can be written by this converter.
boolean canWrite(Class<?> clazz, MediaType mediaType);
// Return the list of MediaType objects supported by this converter.
List<MediaType> getSupportedMediaTypes();
// Read an object of the given type from the given input message, and returns it.
T read(Class<T> clazz, HttpInputMessage inputMessage) throws IOException,
HttpMessageNotReadableException;
// Write an given object to the given output message.
void write(T t, HttpOutputMessage outputMessage) throws IOException,
HttpMessageNotWritableException;
}
使用 <mvc:annotation-driven />标签配置时,默认配置了RequestMappingHandlerAdapter,并为他配置了一下默认的HttpMessageConverter:
ByteArrayHttpMessageConverter: 负责读取二进制格式的数据和写出二进制格式的数据;StringHttpMessageConverter: 负责读取字符串格式的数据和写出二进制格式的数据;- ResourceHttpMessageConverter:负责读取资源文件和写出资源文件数据;
- FormHttpMessageConverter: 负责读取form提交的数据(能读取的数据格式为 application/x-www-form-urlencoded,不能读取multipart/form-data格式数据);负责写入application/x-www-from-urlencoded和multipart/form-data格式的数据;
- MappingJacksonHttpMessageConverter: 负责读取和写入json格式的数据;
- SouceHttpMessageConverter: 负责读取和写入 xml 中javax.xml.transform.Source定义的数据;
- Jaxb2RootElementHttpMessageConverter: 负责读取和写入xml 标签格式的数据;
- AtomFeedHttpMessageConverter: 负责读取和写入Atom格式的数据;
- RssChannelHttpMessageConverter: 负责读取和写入RSS格式的数据;
HttpMessageConverter匹配过程
根据Request对象header部分的ContentType类型和被注解参数类型,逐一匹配合适的HttpMessageConverter来读取数据;
protected <T> Object readWithMessageConverters(HttpInputMessage inputMessage, MethodParameter parameter,
Type targetType) throws IOException, HttpMediaTypeNotSupportedException, HttpMessageNotReadableException {
......
MediaType contentType;
contentType = inputMessage.getHeaders().getContentType();
....... for (HttpMessageConverter<?> converter : this.messageConverters) {
Class<HttpMessageConverter<?>> converterType = (Class<HttpMessageConverter<?>>) converter.getClass();
if (converter instanceof GenericHttpMessageConverter) {
GenericHttpMessageConverter<?> genericConverter = (GenericHttpMessageConverter<?>) converter;
if (genericConverter.canRead(targetType, contextClass, contentType)) {
if (logger.isDebugEnabled()) {
logger.debug("Read [" + targetType + "] as \"" + contentType + "\" with [" + converter + "]");
}
if (inputMessage.getBody() != null) {
inputMessage = getAdvice().beforeBodyRead(inputMessage, parameter, targetType, converterType);
body = genericConverter.read(targetType, contextClass, inputMessage);
body = getAdvice().afterBodyRead(body, inputMessage, parameter, targetType, converterType);
}
else {
body = getAdvice().handleEmptyBody(null, inputMessage, parameter, targetType, converterType);
}
break;
}
}
else if (targetClass != null) {
if (converter.canRead(targetClass, contentType)) {
if (logger.isDebugEnabled()) {
logger.debug("Read [" + targetType + "] as \"" + contentType + "\" with [" + converter + "]");
}
if (inputMessage.getBody() != null) {
inputMessage = getAdvice().beforeBodyRead(inputMessage, parameter, targetType, converterType);
body = ((HttpMessageConverter<T>) converter).read(targetClass, inputMessage);
body = getAdvice().afterBodyRead(body, inputMessage, parameter, targetType, converterType);
}
else {
body = getAdvice().handleEmptyBody(null, inputMessage, parameter, targetType, converterType);
}
break;
}
}
}
....... return body;
}
注意
- 在一个方法的参数列表中,@RequestBody只能使用一次;
- json字符串中,如果value为""的话,后端对应属性如果是String类型的,那么接受到的就是"",如果是后端属性的类型是Integer、Double等类型,那么接收到的就是null;
- json字符串中,如果value为null的话,后端对应收到的就是null;
- 在传json字符串给后端时,如果某个key没有value的话,要么干脆就不写该key,要么就将value赋值null 或"",不能有类似 {......,"key":,.....}, 这样的写法
SpringMVC @RequestBody的使用的更多相关文章
- SpringMVC @RequestBody接收Json对象字符串 demo
springmvc 的这个 @RequestBody 用得比较少,今天看了一下,还是很方便. @RequestBody 接收类似 [{name: "test"}, {name: & ...
- SpringMVC @RequestBody请求参数在postman中的请求
使用SpringMVC框架,controller使用参数 @RequestBody LoginReq req 注解方式模拟http请求 需要请求header添加一个参数 设置 Header参 ...
- SpringMVC @RequestBody 自动转json Http415错误
转自: http://blog.csdn.net/tiantiandjava/article/details/46125141 项目中想用@RequestBody直接接收json串转成对象 网上查了使 ...
- SpringMVC @RequestBody接收Json对象字符串
其实 @RequestBody接收的是一个Json对象的字符串,而不是一个Json对象.然而在ajax请求往往传的都是Json对象,后来发现用 JSON.stringify(data)的方式就能将对象 ...
- SpringMVC@RequestBody小细节
关于@RequestBody 参数类型自己的包装类,还是类似String/int,区别: 1.@RequestBody LoginParmar parmar String user_number = ...
- SpringMVC @RequestBody @RequestParam @PathVariable 等参数绑定注解详解
request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用: http://blog.csdn.net/walkerjong/article/details/794 ...
- SpringMVC @RequestBody 接收Json数组对象
@RequestMapping(value="/signIn",method=RequestMethod.POST) public int saveUser(@RequestBod ...
- SpringMVC @RequestBody问题:Unrecognized field , not marked as ignorable
http://blog.csdn.net/isea533/article/details/33397735
- 【Spring学习笔记-MVC-6】SpringMVC 之@RequestBody 接收Json数组对象
作者:ssslinppp 1. 摘要 程序流程: 前台使用ajax技术,传递json字符串到后台: 后台使用Spring MVC注解@RequestBody 接受前台传递的json字符串, ...
随机推荐
- PAT A1153 Decode Registration Card of PAT (25 分)——多种情况排序
A registration card number of PAT consists of 4 parts: the 1st letter represents the test level, nam ...
- Android开发之加载GIF图片
一.加载GIF图片我用的是GitHub上的开源库:android-gif-drawable,项目地址:https://github.com/koral--/android-gif-drawable 二 ...
- java 面向对象内部类
1.内部类概念:将一个类定义在另一个类的内部,该类就称为内部类 类中定义的内部类特点1) 内部类作为外部类的成员,可以直接访问外部类的成员(包括 private 成员),反之则不行.2) 内部类做为外 ...
- Subversion 1.8.9 ( SVN Client ) 安装最新版本的svn客户端
For CentOS7 Users: [WandiscoSVN] name=Wandisco SVN Repo baseurl=http://opensource.wandisco.com/cento ...
- mysql无法远程连接到数据库解决方法
ERROR 1130: Host ’xxx.xxx.xxx.xxx′ is not allowed to connect to this MySQL server这是告诉你没有权限连接指定IP的主机, ...
- POJ3301 Texas Trip 计算几何、随机化贪心
传送门--Vjudge 三分写法似乎有问题,可以去Udebug上看Morass的\(666\)个测试点的数据,我的乱搞有很多比正解答案小,但还是能在SPOJ和POJ过,可见数据之水. 可以对正方形的角 ...
- ASP.NET MVC中jQuery与angularjs混合应用传参并绑定数据
要求是这样子的,在一个列表页中,用户点击详细铵钮,带记录的主键值至另一页.在另一外页中,获取记录数据,然后显示此记录数据在网页上. 先用动图演示: 昨天有分享为ng-click传递参数 <ang ...
- 【转】JS中,中国标准时间转化为yyyy-MM-dd
'Thu May 12 2016 08:00:00 GMT+0800 (中国标准时间)'--此格式日期无法传到java后台,须格式化,方法如下 var d = new Date('Thu May 12 ...
- aurora 64B/66B ip核设置与例程代码详解
见网页https://blog.csdn.net/u014586651/article/details/84349328 https://blog.csdn.net/u012135070/articl ...
- Python从菜鸟到高手(1):初识Python
1 Python简介 1.1 什么是Python Python是一种面向对象的解释型计算机程序设计语言,由荷兰人吉多·范罗苏姆(Guido van Rossum)于1989年发明,第一个公开发行版 ...