前端数据精度问题
精度丢失
从图中我们可以看到尾数从832变成了800,这个不是后端传输的问题,是由于前端js的精度有限,返回的数据长度过大,导致Json解析出现问题。
解决Long精度丢失
主要是后端通过jackson将长整型的数字转换为字符串返回。
/**
* Spring MVC Json 配置
*/
@JsonComponent
public class JsonConfig {
/**
* 添加 Long 转 json 精度丢失的配置
*/
@Bean
public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
ObjectMapper objectMapper = builder.createXmlMapper(false).build();
SimpleModule module = new SimpleModule();
module.addSerializer(Long.class, ToStringSerializer.instance);
module.addSerializer(Long.TYPE, ToStringSerializer.instance);
objectMapper.registerModule(module);
return objectMapper;
}
}
贡献者
flycodeu
版权所有
版权归属:flycodeu