首页 新闻 会员 周边

springmvc中的@RequestBody接收对象问题

0
[已解决问题] 解决于 2016-04-29 09:54

springmvc中,@RequestBody接收的是一个Json对象?还是一个json对象的字符串?

求大神能详细解答下原因

IT丶逝去的青春的主页 IT丶逝去的青春 | 初学一级 | 园豆:33
提问于:2016-03-16 15:45
< >
分享
最佳答案
0

官方API里面这样说:

Annotation indicating a method parameter should be bound to the body of the web request. The body of the request is passed through an HttpMessageConverter to resolve the method argument depending on the content type of the request.

我理解的是,它与传输时候指定的方法contentType有关,Spring会尝试按照指定的文档类型去解析。

如果指定为json,那么Spring尝试会用JSON去解析;如果是context/plain就是字符串。

 

况且,一般在使用@requestBody都会跟着数据类型。如果你的参数是(@requestBody String req),那不用说了,肯定是字符串。

 

一般来说,我都是设置type为 context/plain,然后在服务器端通过fastjson来解析。

奖励园豆:5
xingoo | 小虾三级 |园豆:711 | 2016-03-17 17:57
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册