今天使用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. python 内置方法join 给字符串加分隔符

    #!/usr/bin/python3 # -*- coding: utf-8 -*- test = "今天吃了吗" test = "_".join(test) ...

  2. 【RPC】综述

    RPC定义 RPC(Remote Procedure Call)全称远程过程调用,它指的是通过网络,我们可以实现客户端调用远程服务端的函数并得到返回结果.这个过程就像在本地电脑上运行该函数一样,只不过 ...

  3. 【LeetCode每天一题】Reverse Linked List(链表反转)

    Reverse a singly linked list. Example:           Input: 1->2->3->4->5->NULL          ...

  4. 修改CentOS的IP地址

    一.临时修改 命令:ifconfig eth0 192.168.1.147 重启或者关机后,iP地址将会恢复到修改之前的状态. 二.永久修改 命令: vi /etc/sysconfig/network ...

  5. 关于RBAC的文章

    权限系统与RBAC模型概述 RBAC权限管理模型 摘自慕课网的RBAC

  6. [xdoj] 1310 DSKer的卡牌游戏

    http://acm.xidian.edu.cn/problem.php?id=1310 1. 这道题可以类比括号匹配,YY和yy是两组可以匹配的信号,当然要注意逻辑是否正确,一开始进行括号匹配算法的 ...

  7. React对比Vue(一些小细节的差异)

    @1===>发现一个神奇的地方在对数组进行增加删除的时候 react中一个输入框点击enter键,然后数组push,然后渲染 <input ref='valInput' onKeyUp={ ...

  8. Delegate,Action,Func,匿名方法,匿名委托,事件

    一.委托Delegate 一般的方法(Method)中,我们的参数总是string,int,DateTime...这些基本的数据类型(或者没有参数),比如 public void HelloWorld ...

  9. 软件常用设置(VC, eclipse ,nodejs)---自己备用

    留存复制使用 1.VC ----1.1VC项目设置 输出目录: $(SolutionDir)../bin/$(platform)/$(Configuration) $(ProjectDir)../bi ...

  10. python django简单的登陆实现

    实现方法: 1,可以先定义一个基础的页面访问路径 例如:http://127.0.0.1:8000/index/  定义index路径 在urls urlpatterns = [ url(r'^ind ...