本文作者:程序员飞云
精度丢失问题
在前端接受Long类型的数据时,发现精度丢失了,但是实际上数据是没问题的,这个可以在前端处理也可以在后端处理。
后端处理
主要就是使用jackson将Long类型转换为String给前端
import com.fasterxml.jackson.databind.ObjectMapper;import com.fasterxml.jackson.databind.module.SimpleModule;import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;import org.springframework.boot.jackson.JsonComponent;import org.springframework.context.annotation.Bean;import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
/** * Spring MVC Json 配置 */@JsonComponentpublic 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; }}
评论