java中Long的取值范围:(-9223372036854774808~9223372036854774807),占用8个字节(-2的63次方到2的63次方-1)
js中Number的范围:-2^53 ------ 2^53
显然,这里会出现精度丢失的问题,给出两个解决方案:
1)Long转为String,再塞进json,这个方法比较粗暴;
2)使用@JsonSerialize注解(https://www.cnblogs.com/luxianyu-s/p/9640588.html)
@Data
public class BasePageResInfo<T> {
private T data;
@JsonSerialize(using=LongConvString.class)
private String nextSpnum;//查看更多,翻页功能
public BasePageResInfo(T rows, String nextSpnum) {
this.data = rows;
this.nextSpnum = nextSpnum;
}
}
public class LongConvString extends JsonSerializer<Long> {
@Override
public void serialize(Long aLong, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
jsonGenerator.writeString(aLong.toString());
}
}
网友评论