springboot使⽤fastJson处理时间
spring boot 使⽤fastJson 处理时间
⼀、需要在vo中时间字段上加注解
@JSONField(format ="yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
⼆、对fastjson设置
@Configuration
public class FastjsonConfiguration extends WebMvcConfigurationSupport {
@Override
protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
final FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
final FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(
SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteNullNumberAsZero,
fastjson怎么用SerializerFeature.WriteNullListAsEmpty,
SerializerFeature.WriteNullStringAsEmpty,
SerializerFeature.WriteNullBooleanAsFalse,
SerializerFeature.WriteDateUseDateFormat,
SerializerFeature.PrettyFormat,
SerializerFeature.WriteEnumUsingToString,
SerializerFeature.DisableCircularReferenceDetect);
final List<MediaType> fastJsonMediaType = new ArrayList<>();
fastJsonMediaType.add(MediaType.APPLICATION_JSON_UTF8);
fastJsonHttpMessageConverter.setSupportedMediaTypes(fastJsonMediaType);
fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
converters.add(fastJsonHttpMessageConverter);
}
}
三、使⽤fastJson 后,controller层在接收参数时,会有Long值精度丢失的问题,需要在上⾯的⽅法中加上如下配置
//解决精度丢失问题----start
SerializeConfig serializeConfig = SerializeConfig.globalInstance;
serializeConfig.put(BigInteger.class, ToStringSerializer.instance);
serializeConfig.put(Long.class, ToStringSerializer.instance);
serializeConfig.put(Long.TYPE, ToStringSerializer.instance);
fastJsonConfig.setSerializeConfig(serializeConfig);
fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
//解决精度丢失问题----end
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论