Java Long类型处理精度丢失问题
Posted wb54979
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java Long类型处理精度丢失问题相关的知识,希望对你有一定的参考价值。
方法一、使用JsonSerialize
注解序列化的时候把Long自动转为String
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
@JsonSerialize(using = ToStringSerializer.class)
private Long id;
方法二、使用JsonSerialize
注解序列化的时候把Long自动转为String
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
@Configuration
@AutoConfigureAfter(JacksonAutoConfiguration.class)
public class JacksonConfiguration
public JacksonConfiguration(ObjectMapper objectMapper)
SimpleModule module = new SimpleModule();
module.addSerializer(new ToStringSerializer(Long.TYPE));
module.addSerializer(new ToStringSerializer(Long.class));
module.addSerializer(new ToStringSerializer(BigInteger.class));
objectMapper.registerModule(module);
以上是关于Java Long类型处理精度丢失问题的主要内容,如果未能解决你的问题,请参考以下文章