fastJson的配置时间序列化格式
fastJson的配置时间格式化,如果在config中配置的话,所有的bean都会统⼀格式,有的项⽬灵活格式。
1.JSONObject.DEFFAULT_DATE_FORMAT="yyyy-MM-dd";//设置⽇期格式
JSONString(resultMap, SerializerFeature.WriteMapNullValue,SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteDateUseDateFormat);
但是上⾯的解决⽅案⾯临⼀个问题,如果不满⾜上⾯的条件(多个date属性,⽽且需要按照不定的格式序列化这些⽇期属性),那么我们就需要另辟蹊径,使⽤fastjson的特性来完成:
@JSONField(format="yyyyMMdd")
private Date date;
@JSONField(format="yyyy-MM-dd HH:mm:ss")
private Date date1;
配置⽂件: 其中configureMessageConverters 配置了消息转换,fastJsonConfig.setDateFormat(“yyyy-MM-dd HH:mm:ss”);统⼀样式。
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd", timezone = "GMT+8")
和 jastJson冲突。
web.interceptor;
util.prop.WebDefineProperties;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fig.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import t.annotation.Configuration;
import org.springframework.http.MediaType;
import org.verter.HttpMessageConverter;
import org.verter.json.MappingJackson2HttpMessageConverter;
import org.springframework.fig.annotation.CorsRegistry;
import org.springframework.fig.annotation.ResourceHandlerRegistry;
import org.springframework.fig.annotation.WebMvcConfigurer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@Configuration
public class WebMvcConfgInterceptor implements WebMvcConfigurer {
@Autowired
private WebDefineProperties wdp;
/**
* 静态资源
fastjson忽略属性
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static");
registry.addResourceHandler("/swagger/**").addResourceLocations("classpath:/static/swagger/");
registry.addResourceHandler("/upload/**").addResourceLocations("file:" +AudioPath());
WebMvcConfigurer.super.addResourceHandlers(registry);
}
/
**
* 消息转换
*/
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
Iterator<HttpMessageConverter<?>> iterator = converters.iterator();
while (iterator.hasNext()) {
HttpMessageConverter<?> converter = ();
HttpMessageConverter<?> converter = ();
if (converter instanceof MappingJackson2HttpMessageConverter) {
}
}
FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
//  fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat, SerializerFeature.QuoteFieldNames,
//    SerializerFeature.WriteEnumUsingToString, SerializerFeature.WriteDateUseDateFormat,
//    SerializerFeature.DisableCircularReferenceDetect);
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat, SerializerFeature.QuoteFieldNames,
SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteDateUseDateFormat,
SerializerFeature.DisableCircularReferenceDetect);
//fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
List<MediaType> fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
converters.add(fastJsonHttpMessageConverter);
figureMessageConverters(converters);
}
/**
* 跨域操作
*/
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("*").allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")    .allowCredentials(false).maxAge(3600);
WebMvcConfigurer.super.addCorsMappings(registry);
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。