今天使用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的更多相关文章

  1. java后台接收json数据,报错com.alibaba.fastjson.JSONObject cannot be cast to xxx

    从前台接收json封装的list数据,在后台接收时一直报错,com.alibaba.fastjson.JSONObject cannot be cast to xxx, 使用这种方式接收可以接收 @R ...

  2. fastjson的坑 com.alibaba.fastjson.JSONObject cannot be cast to xxx

    解析json对象时,使用了new TypeReference()对象 fastjson会对解析的对象类型进行缓存   new TypeReference<ResultData>(){}   ...

  3. 42-字符串到json 的错误 com.alibaba.fastjson.JSONObject cannot be cast to java.lang.String

    json: {"updated_at":1551780617,"attr":{"uptime_h":3,"uptime_m&quo ...

  4. No message body writer has been found for class com.alibaba.fastjson.JSONObject, ContentType: */*

    1:当使用 cxf 发布服务时,要求返回值类型为xml,或者json等 @Path("/searchProductByText") @GET @Produces({"ap ...

  5. net.sf.json.JSONOBJECT.fromObject 与 com.alibaba.fastjson.JSONObject.parseObject

    文章待补充,先写写以下知识点好了. NULL值处理之 net.sf.json.JSONObject 和 com.alibaba.fastjson.JSONObject区别 JSON作为一个轻量级的文本 ...

  6. com.alibaba.fastjson.JSONObject循环给同一对象赋值会出现"$ref":"$[0]"现象问题

    1.今天定义了一个JSONObject对象,引用的com.alibaba.fastjson.JSONObject,循环给这个对象赋值出现"$ref":"$[0]" ...

  7. com.alibaba.fastjson.JSONObject之对象与JSON转换方法

    com.alibaba.fastjson.JSONObject时经常会用到它的转换方法,包括Java对象转成JSON串.JSON对象,JSON串转成java对象.JSON对象,JSON对象转换Java ...

  8. com.alibaba.fastjson.JSONObject;的使用

    转: com.alibaba.fastjson.JSONObject;的使用 2018-11-04 23:51:23 mameng1998 阅读数 6404更多 分类专栏: java   1  POM ...

  9. Java-Class-I:com.alibaba.fastjson.JSONObject

    ylbtech-Java-Class-I:com.alibaba.fastjson.JSONObject 1.返回顶部 1.1.import com.alibaba.fastjson.JSON;imp ...

随机推荐

  1. AOP 入门

    1,源码 Application.java package com.bf; import org.springframework.context.ApplicationContext; import ...

  2. 3-Python3 环境搭建

  3. [LeetCode] 100. Same Tree_Easy tag: DFS

    Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...

  4. python浮窗

    import wx; app = wx.App(); win = wx.Frame(None,title="老穆视频",pos=(900,20),size=(300,60),sty ...

  5. PS教程:如何批量处理图片

    1.我们先准备两个文件夹,一个用来装你要处理的图片,可以是几百上千张,另一个是空文件夹,用来装等下处理好的图片. 2.打开PS,打开未处理文件夹里的任何一张图片. 3. 在红圈中点击,新建一个动作. ...

  6. (转载)Spring定时任务的几种实现

    spring框架定时任务 一.分类 从实现的技术上来分类,目前主要有三种技术(或者说有三种产品): Java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerT ...

  7. 有关于异常捕获点滴,plus我也揭揭java的短

    ▄︻┻┳═一『异常捕获系列』Agenda: ▄︻┻┳═一有关于异常捕获点滴,plus我也揭揭java的短 ▄︻┻┳═一根据异常自定义处理逻辑([附]java异常处理规范) ▄︻┻┳═一利用自定义异常来 ...

  8. MyBatis基础入门《十四》ResultMap子元素(association )

    MyBatis基础入门<十四>ResultMap子元素(association ) 1. id: >> 一般对应数据库中改行的主键ID,设置此项可以提高Mybatis的性能 2 ...

  9. Unity3d之表情动画--眨眼

    可通过BlendShape来实现眨眼动画,效果如下: 转载请注明出处:http://www.cnblogs.com/jietian331/p/7054673.html 代码如下: using Unit ...

  10. Necklace of Beads (polya定理的引用)

    Beads of red, blue or green colors are connected together into a circular necklace of n beads ( n &l ...