解决SpringMVC使用fastJson后Long类型丢失精度的问题

原来项目不是用fastjson格式化,用的是fasterxml

public void extendMessageConverters(List> converters) {
        for (HttpMessageConverter c : converters) {
            if (c instanceof MappingJackson2HttpMessageConverter) {
                MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = (MappingJackson2HttpMessageConverter) c;
                ObjectMapper objectMapper = mappingJackson2HttpMessageConverter.getObjectMapper();
                objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
                if (this.dateFormat != null) {
                    DateFormat myDateFormat = new SimpleDateFormat(dateFormat);
                    objectMapper.setDateFormat(myDateFormat);
                }

                // 注册Long专用的json转换器
                SimpleModule module = new SimpleModule();
                //修复Long类型太长,丢失精度问题
                module.addSerializer(Long.class, com.fasterxml.jackson.databind.ser.std.ToStringSerializer.instance);
                module.addSerializer(Long.TYPE, com.fasterxml.jackson.databind.ser.std.ToStringSerializer.instance);
                module.addSerializer(BigDecimal.class, com.fasterxml.jackson.databind.ser.std.ToStringSerializer.instance);
                module.addSerializer(BigInteger.class, com.fasterxml.jackson.databind.ser.std.ToStringSerializer.instance);
                objectMapper.registerModule(module);
            }
        }
    }

但后面要求全部用fastJson,pom改为

  
            org.springframework.boot
            spring-boot-starter-web
            
                
                
                    com.fasterxml.jackson.core
                    jackson-databind
                
            
        

这样,原来序列化的问题,也来了  
解决办法如下:

  • 生成序列化的转换类ToStringSerializer
public class ToStringSerializer implements ObjectSerializer {

    public static final ToStringSerializer instance = new ToStringSerializer();

    @Override
    public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType,
                      int features) throws IOException {
        SerializeWriter out = serializer.out;

        if (object == null) {
            out.writeNull();
            return;
        }

        String strVal = object.toString();
        out.writeString(strVal);
    }
}

  • ToStringSerializer的Web配置
 @Bean
    public HttpMessageConverters getConverters() {
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        FastJsonConfig config = new FastJsonConfig();
        config.setSerializerFeatures(
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.QuoteFieldNames,
                SerializerFeature.IgnoreNonFieldGetter
            );
        SerializeConfig serializeConfig = new CustomSerializeConfig();
        serializeConfig.put(BigInteger.class, ToStringSerializer.instance);
        serializeConfig.put(Long.class, ToStringSerializer.instance);
        serializeConfig.put(Long.TYPE, ToStringSerializer.instance);
        config.setSerializeConfig(serializeConfig);
        converter.setFastJsonConfig(config);
        List mediaTypes = Arrays.asList(
                MediaType.APPLICATION_JSON_UTF8,
                MediaType.TEXT_PLAIN,
                MediaType.TEXT_HTML,
                MediaType.TEXT_XML,
                MediaType.APPLICATION_OCTET_STREAM);
        converter.setSupportedMediaTypes(mediaTypes);

        return new HttpMessageConverters(converter);
    }

按以上所示,注册序列化类型,经测试,功能正常!

你可能感兴趣的:(解决SpringMVC使用fastJson后Long类型丢失精度的问题)