Long数据类型序列化Json后传递给前端,产生的精度丢失的问题解决
package baselib.cunyt.cn.common.mvc; 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.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; import java.math.BigInteger; /** * 解决java Long转json精度丢失的问题 * * @author wupeiguo */ @Configuration public class ApiMvcConfiguration { @Bean @Primary @ConditionalOnMissingBean(ObjectMapper.class) public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) { ObjectMapper objectMapper = builder.createXmlMapper(false).build(); // 全局配置序列化返回 JSON 处理 SimpleModule simpleModule = new SimpleModule(); //JSON Long ==> String //超长整 simpleModule.addSerializer(BigInteger.class, ToStringSerializer.instance); //长整 simpleModule.addSerializer(Long.class,ToStringSerializer.instance); //长整 simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance); objectMapper.registerModule(simpleModule); return objectMapper; } }
转载自:
https://blog.csdn.net/YangYF1997/article/details/119828164
https://www.csdn.net/tags/Mtjagg0sMDI1NzMtYmxvZwO0O0OO0O0O.html

浙公网安备 33010602011771号