Springboot项目解决前端Long字段精度丢失问题
一、概述
Springboot项目后端把Long类型的数据传给前端,前端可能会出现精度丢失的情况,解决方案是将Long字段转成字符串。 转换为
二、jackson解决方案
1.局部解决
在字段上添加@JsonSerialize(using = ToStringSerializer.class)
2.全局解决
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class JacksonConfig { @Bean public ObjectMapper objectMapper() { ObjectMapper objectMapper = new ObjectMapper(); SimpleModule simpleModule = new SimpleModule(); simpleModule.addSerializer(Long.class, ToStringSerializer.instance) .addSerializer(Long.TYPE, ToStringSerializer.instance); objectMapper.registerModule(simpleModule); return objectMapper; } }