fastJson的配置时间序列化格式
fastJson的配置时间格式化,如果在config中配置的话,所有的bean都会统一格式,有的项目灵活格式。1.JSONObject.DEFFAULT_DATE_FORMAT="yyyy-MM-dd";//设置日期格式2.JSONObject.toJSONString(resultMap, SerializerFeature.WriteMapNullValue,SerializerFeature
·
fastJson的配置时间格式化,如果在config中配置的话,所有的bean都会统一格式,有的项目灵活格式。
1.JSONObject.DEFFAULT_DATE_FORMAT="yyyy-MM-dd";//设置日期格式
2.JSONObject.toJSONString(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冲突。
package cn.etcom.web.interceptor;
import cn.etcom.util.prop.WebDefineProperties;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@Configuration
public class WebMvcConfgInterceptor implements WebMvcConfigurer {
@Autowired
private WebDefineProperties wdp;
/**
* 静态资源
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static");
registry.addResourceHandler("/swagger/**").addResourceLocations("classpath:/static/swagger/");
registry.addResourceHandler("/upload/**").addResourceLocations("file:" +wdp.getAudioPath());
WebMvcConfigurer.super.addResourceHandlers(registry);
}
/**
* 消息转换
*/
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
Iterator<HttpMessageConverter<?>> iterator = converters.iterator();
while (iterator.hasNext()) {
HttpMessageConverter<?> converter = iterator.next();
if (converter instanceof MappingJackson2HttpMessageConverter) {
iterator.remove();
}
}
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);
WebMvcConfigurer.super.configureMessageConverters(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);
}
}
更多推荐
已为社区贡献2条内容
所有评论(0)