数字类型字段长度大于17位时,前端会出现精度丢失。
解决方法:返回前端时将该类型字段转为字符串
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.Configuration;
import java.math.BigInteger;
/**
* 数字类型字段长度大于17位时,前端会出现精度丢失。
* 解决方法:返回前端时将该类型字段转为字符串
*/
@Configuration
public class LongNumberFieldConfig implements InitializingBean {
@Resource
private ObjectMapper objectMapper;
@Override
public void afterPropertiesSet() throws Exception {
SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
simpleModule.addSerializer(BigInteger.class, ToStringSerializer.instance);
objectMapper.registerModule(simpleModule);
}
}
评论区