探索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 ...
随机推荐
- ssh tunnel 三种模式
环境介绍: 主机 位置 公网 内网IP 角色 host-a 局域网1 否 192.168.0.1 ssh client host-b 局域网2.公网 是 192.168.1.1 ssh server ...
- 3-Python3 环境搭建
- Sqlserver2008R2配置数据库镜像之我的经验总结
一. 相关环境介结. 数据库:Sqlserver2008R2 网络环境:主机.镜像机(阿里云,青岛节点同域),见证机(本公司自己托管在上海) 二. 服务器相关配置. 1. 分别开启三台服务器50 ...
- lambda 表达式拼接
类库: using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions ...
- 使用github(一)
一.使用Github(目的.基本概念) 1.目的 借助github托管项目代码 2.基本概念 (1)仓库(Repository) 仓库即项目的意思,你想在github上开源一个项目,那就必须要新建一个 ...
- ROC曲线,AUC面积
AUC(Area under Curve):Roc曲线下的面积,介于0.1和1之间.Auc作为数值可以直观的评价分类器的好坏,值越大越好. 首先AUC值是一个概率值,当你随机挑选一个正样本以及负样本, ...
- OAuth2.0标准类库汇总
转载官网: https://oauth.net/code/ https://www.w3cschool.cn/oauth2/5ghz1jab.html 服务端类库 .NET .NET DotNetOp ...
- Redis:C#使用Redis(1)
一.安装 1.下载安装包: 官方网站:redis.io 官方推荐windows版本:https://github.com/MSOpenTech/redis 2:下载压缩包,解压后如下 redis-se ...
- node+react 打包成功,控制台报错
控制台报错: 'ReactCurrentOwner' of undefined 解决办法:RN版本的问题. As I mentioned, make sure you've installed the ...
- 转 VS2010 RDLC 横向合并时“未正确设置 tablix“Tablix1”的 FixedData 属性”错误解决方法 .
最近在使用Rdlc做报表打印,有些报表的表头需要合并表头.Rdlc本身提供了横向合并的工具,但是在实际合并的时候,会出现“未正确设置 tablix“Tablix1”的 FixedData 属性.除非在 ...