/** * @Auther: mxf * @Date: 2019/4/18 09:12 * @Description: */ @Configuration @EnableWebMvc public class WebConfig implements WebMvcConfigurer { @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter(); FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializeFilters(new ValueFilter() { @Override public Object process(Object object, String name, Object value) { if (value == null){ return ""; } if (value instanceof Date){ return DateUtils.formatDate((Date) value,"yyyy-MM-dd hh:mm:ss"); } return value; } }); fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig); List<MediaType> mediaTypes = new ArrayList<>(); mediaTypes.add(MediaType.APPLICATION_JSON_UTF8); fastJsonHttpMessageConverter.setSupportedMediaTypes(mediaTypes); converters.add(fastJsonHttpMessageConverter); } }
通过序列化过滤器将值为null的换成空字符串,然后对日期类型的进行格式化。