springboot整合fastjson 将null转成空字符串

/**
 * @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的换成空字符串,然后对日期类型的进行格式化。

posted @ 2019-04-18 11:32  xfma  阅读(3699)  评论(0编辑  收藏  举报