正文
JavaScript中Number类型并不能完全表示Long型的数字,在Long长度大于17位时会出现精度丢失的问题,超过17位的部分浏览器会转换为0显示
js中的Number类型只能解析其中的17位,其他几位补位为0,就会造成如上问题。
解决办法
把long类型的字段转换为String类型给前端
@JSONField(serializeUsing = Long2StringJsonSerialize.class)
private Long id;
public class Long2StringJsonSerialize implements ObjectSerializer {
@Override
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType,
int features) throws IOException {
SerializeWriter out = serializer.out;
if (object == null) {
out.writeNull();
return;
}
String strVal = object.toString();
out.writeString(strVal);
}
}
把对象拷贝到VO类
List<UserConvertVO> userConverts = new ArrayList<>();
if (EmptyUtil.notEmpty(data)) {
for (User record : data.getRecords()) {
UserConvertVO userConvertVO = new UserConvertVO();
BeanUtil.copyProperties(record, userConvertVO);
userConverts.add(userConvertVO);
}
}