问题
比如后端返回Long.MAX_VALUE(9,223,372,036,854,775,807),前端得到的却是9,223,372,036,854,776,000;后4位精度损失
Screen Shot 2020-04-30 at 10.40.13 AM.png
Screen Shot 2020-04-30 at 10.40.25 AM.png
分析
- JavaScript中Number.MAX_SAFE_INTEGER为9007199254740991(正负2^53-1)
- Java 中Long的最大值为9,223,372,036,854,775,807(2^63 -1)
所以JavaScript无法接受所有Long类型数据。
解决方案
如果Java后端中将要返回的值是Long类型,建议将Long转换成String类型
package com.mervyn.filterdemo;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class User {
@JsonSerialize(using = ToStringSerializer.class)
private Long id;
private String name;
public User(Long id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
Screen Shot 2020-04-30 at 11.16.10 AM.png
网友评论