@ResponBody和阿里JSON
孙玉超
2020-05-09 15:11:14
评论
1164 浏览
收藏
0 赞
先来看看我们使用@ResponseBody注解拿到的JSON
[
{
"id": 5,
"userId": "1174624540060991489",
"name": "董志祺",
"mobile": "17327766495",
"address": null,
"wechatId": null,
"nickname": "橙工管家-胡锐",
"role": 2,
"createTime": "2019-11-29T07:56:58.000+0000",
"createBy": null,
"updateTime": "2019-11-29T07:56:58.000+0000",
"updateBy": null,
"isDeleted": 0
},
{
"id": "1217991344905699330",
"userId": "1217987611387936769",
"name": "董丽",
"mobile": "18013307731",
"address": null,
"wechatId": null,
"nickname": null,
"role": 1,
"createTime": "2020-01-17T02:05:40.000+0000",
"createBy": null,
"updateTime": "2020-01-17T02:05:40.000+0000",
"updateBy": null,
"isDeleted": 0
}
]注意,这里实体类字段的id和userId属性都是Long类型的,但是在返回的时候却变成了被字符串包起来了。
再来看看如果使用@ResponseBody但是返回String类型的JSON.toJSONString()
[
{
"createTime": 1575014218000,
"id": 5,
"isDeleted": 0,
"mobile": "17327766495",
"name": "董志祺",
"nickname": "橙工管家-胡锐",
"role": 2,
"updateTime": 1575014218000,
"userId": 1174624540060991489
},
{
"createTime": 1579226740000,
"id": 1217991344905699330,
"isDeleted": 0,
"mobile": "18013307731",
"name": "董丽",
"role": 1,
"updateTime": 1579226740000,
"userId": 1217987611387936769
}
]用JSON.toJSONString()转过之后,它会保留原始类型。不会转成字符串。
我以前还真没注意这个问题,直到今天写需求,我向来是习惯 return JSON.toJSONString() 的。前端接收到userId之后再传给我居然变了,而且在浏览器的preview和response两栏居然显示的值不同。百度了一下,网上说是因为浏览器的精度问题,把Long类型的精度丢失了还是什么鬼!!
长见识了,然后我只能。。。把JSON去掉,用@ResponseBody,也就是SpringMVC自带的JSON工具来处理了~~~~
学无止境,你知道的越多,你不知道的也越多