探索RequestBody报com.alibaba.fastjson.JSONObject cannot be cast to xxx
今天使用RequestBody接受前端传过来的参数,以前接受字符串数组非常成功,这次把形参改成了List<User>,原本以为顺利接受参数并映射成User的list结构,结果竟然在我取user.getId()时报了com.alibaba.fastjson.JSONObject cannot be cast to xxx的错。
前端:
$.ajax({
url : "/insertUser",
async : true,
cache : false,
type : "post",
contentType : "application/json; charset=UTF-8",
data : JSON.stringify(userList),
success : function(data) {
//...
}
});
后端:
@RequestMapping("/insertUser")
public void insertBlank(@RequestBody List<User> userList) {
User user = userList.get(0);
System.out.println(user.getId());
}
不知怎的,RequestBody接受参数不能直接转成想要的类,通过debug观察到userList接受到了一个JSONArray<JSONObject>的结构,根本没有转成List<User>.
搜索资料,发现要想用RequestBody直接映射到java对象,需要配置在配置springMVC注解驱动时配置fastJson转换器,看了看项目中的配置文件,这的配了这个东西。
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
但是与资料不同,正在开发的项目还对这个转换器设置了支持触发的类型application/json;charset=UTF-8。
观察一下
发送的请求为application/json; charset=UTF-8,
支持的类型为application/json;charset=UTF-8
发现端倪了,我发的请求类型中间多了一个空格!
去掉空格发送请求,结果:
我的user对象还是没有转换成功,还是一个一个JSONObject,但是请观察,JSONArray转换成了ArrayList。
嗯,配置的映射转换器生效了,结果表明,RequestBody能直接将json对象映射成java对象,但仅限于第一层的对象,至于嵌套的对象,则需要开发者自己去转换。
@RequestMapping("/insertUser")
public void insertUser(@RequestBody List<JSONObject> list) {
List<User> userList = list.stream().map(json -> JSONObject.toJavaObject(json, User.class)).collect(Collectors.toList());
service.insertUser(userList);
}
探索RequestBody报com.alibaba.fastjson.JSONObject cannot be cast to xxx的更多相关文章
- java后台接收json数据,报错com.alibaba.fastjson.JSONObject cannot be cast to xxx
从前台接收json封装的list数据,在后台接收时一直报错,com.alibaba.fastjson.JSONObject cannot be cast to xxx, 使用这种方式接收可以接收 @R ...
- fastjson的坑 com.alibaba.fastjson.JSONObject cannot be cast to xxx
解析json对象时,使用了new TypeReference()对象 fastjson会对解析的对象类型进行缓存 new TypeReference<ResultData>(){} ...
- 42-字符串到json 的错误 com.alibaba.fastjson.JSONObject cannot be cast to java.lang.String
json: {"updated_at":1551780617,"attr":{"uptime_h":3,"uptime_m&quo ...
- No message body writer has been found for class com.alibaba.fastjson.JSONObject, ContentType: */*
1:当使用 cxf 发布服务时,要求返回值类型为xml,或者json等 @Path("/searchProductByText") @GET @Produces({"ap ...
- net.sf.json.JSONOBJECT.fromObject 与 com.alibaba.fastjson.JSONObject.parseObject
文章待补充,先写写以下知识点好了. NULL值处理之 net.sf.json.JSONObject 和 com.alibaba.fastjson.JSONObject区别 JSON作为一个轻量级的文本 ...
- com.alibaba.fastjson.JSONObject循环给同一对象赋值会出现"$ref":"$[0]"现象问题
1.今天定义了一个JSONObject对象,引用的com.alibaba.fastjson.JSONObject,循环给这个对象赋值出现"$ref":"$[0]" ...
- com.alibaba.fastjson.JSONObject之对象与JSON转换方法
com.alibaba.fastjson.JSONObject时经常会用到它的转换方法,包括Java对象转成JSON串.JSON对象,JSON串转成java对象.JSON对象,JSON对象转换Java ...
- com.alibaba.fastjson.JSONObject;的使用
转: com.alibaba.fastjson.JSONObject;的使用 2018-11-04 23:51:23 mameng1998 阅读数 6404更多 分类专栏: java 1 POM ...
- Java-Class-I:com.alibaba.fastjson.JSONObject
ylbtech-Java-Class-I:com.alibaba.fastjson.JSONObject 1.返回顶部 1.1.import com.alibaba.fastjson.JSON;imp ...
随机推荐
- 3-Python3 环境搭建
- CentOS6.5安装Elasticsearch1.7.5
1. 首页到官方网站下载最新安装包 https://www.elastic.co/downloads/elasticsearch elasticsearch-1.7.5.tar.gz 2. 将软件包 ...
- leetcode 22括号生成
非常好的一道题.一开始的思想是这样的,先把n对括号按照某一顺序生成一个string,然后用全排列算法生成所有可能,然后利用stack写一段判断括号是否匹配的字符串,匹配的假如结果中.不过会超时.因为全 ...
- 彻底理解什么是原型链,prototype和__proto__的区别以及es5中的继承
再讲一遍好了( 参考https://blog.csdn.net/cc18868876837/article/details/81211729 https://blog.csdn.net/lc23742 ...
- Linux 7.x 设置主机名称
Linux 7.x 设置主机名称 在Linux7.x 版本中,临时设置主机名称使用指令:hostnamectrl set-name 主机名称 [root@localhost ~]# hostname ...
- C#日期格式字符串的相互转换
方法一:Convert.ToDateTime(string) string格式有要求,必须是yyyy-MM-dd hh:mm:ss ================================== ...
- Mysql8.0.11安装以及注意事项
一.环境配置 首先在官网下载最新的mysql8.0.11数据库,解压到你需要放置的盘符最好不要有中文,然后新建MYSQL_HOME,参数为mysql解压后安装文件的bin文件路径如我的:变量名:MYS ...
- 20170918-00-(代理ip检验)
代码集编号 20170918-00 import random #随机数模块 import urllib.request #常用爬虫模块 import time from bs4 import Bea ...
- python 文件写入错误
在保存网页文字到txt文件下时,出现如下错误 UnicodeEncodeError: 'gbk' codec can't encode character u'\xa9' in position 24 ...
- keras tensorboard的使用
http://blog.csdn.net/xiaojiajia007/article/details/72865764 https://stackoverflow.com/questions/4211 ...