<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[Heck's  Blog]]></title> 
<link>https://www.heckjj.com/index.php</link> 
<description><![CDATA[一瞬间的决定，往往可以改变很多，事实上，让自己成功的往往不是知识，是精神！ 如果你总是为自己找借口，那只好让成功推迟。执行力，今天！]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[Heck's  Blog]]></copyright>
<item>
<link>https://www.heckjj.com/post//</link>
<title><![CDATA[RequestBody报com.alibaba.fastjson.JSONObject cannot be cast to]]></title> 
<author>Heck &lt;@hecks.tk&gt;</author>
<category><![CDATA[编程杂谈]]></category>
<pubDate>Tue, 07 Jan 2020 07:18:31 +0000</pubDate> 
<guid>https://www.heckjj.com/post//</guid> 
<description>
<![CDATA[ 
	今天使用RequestBody接受前端传过来的参数，以前接受字符串数组非常成功，这次把形参改成了List<User>，原本以为顺利接受参数并映射成User的list结构，结果竟然在我取user.getId()时报了com.alibaba.fastjson.JSONObject cannot be cast to xxx的错。<br/><br/>后端：<br/> @RequestMapping("/insertUser")<br/> public void insertBlank(@RequestBody List<User> userList) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp; User user = userList.get(0);<br/>&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(user.getId());<br/> &#125;<br/><br/><br/>我的user对象没有转换成功，还是一个一个JSONObject，但是请观察，JSONArray转换成了ArrayList。<br/><br/>　　嗯，配置的映射转换器生效了，结果表明，RequestBody能直接将json对象映射成java对象，但仅限于第一层的对象，至于嵌套的对象，则需要开发者自己去转换。<br/><br/>@RequestMapping("/insertUser")<br/> public void insertUser(@RequestBody List<JSONObject> list) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;List<User> userList = list.stream().map(json -> JSONObject.toJavaObject(json, User.class)).collect(Collectors.toList());<br/>&nbsp;&nbsp; service.insertUser(userList);<br/> &#125;
]]>
</description>
</item><item>
<link>https://www.heckjj.com/post//#blogcomment</link>
<title><![CDATA[[评论] RequestBody报com.alibaba.fastjson.JSONObject cannot be cast to]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>https://www.heckjj.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>