一开始我是以对象形式把数据从后台返回到前端,追踪发现从后台返回了有两条数据,但是前端只显示了一条数据,如图。
{"code":0,"count":2,"data":[{"boxId":1069786974417637377,"containerId":1069786974417637377,"height":0.0,"id":"1069786974417637377","length":0.0,"placeOrder":1,"width":0.0,"x":0.0,"y":0.0,"z":0.0},{"boxId":1069786974417637377,"containerId":1069786974417637377,"height":0.0,"id":"1069786974417637378","length":13.9568,"placeOrder":2,"width":0.0,"x":0.0,"y":0.0,"z":0.0}],"message":"请求成功"}
说明数据获取正常,刚好今天遇到了一个类似的问题,从后台返回到前端的箱子id和容器id的精度丢失了,网上对于精度丢失问题的解决办法是把long类型的数据转换成String类型的数据。因此double数据在数据表格里面无法显示出来的问题也可以这样解决。使用阿里巴巴的fastjson,导入这两个包
import com.alibaba.fastjson.annotation.JSONField; import com.alibaba.fastjson.serializer.ToStringSerializer;
在需要把相应属性转换为String类型的地方加上
@JSONField(serializeUsing= ToStringSerializer.class)注解,这样使用JSON.toJSONString就可以自动进行类型转换。问题解决。
贴部分相关代码
<!-- 阿里巴巴fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.51</version>
</dependency>
public class SuperEntity<T extends Model>{
@JSONField(serializeUsing= ToStringSerializer.class)
private Long id;
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
}
String str = JSON.toJSONString(entity);