springboot 通过FastJson + 配置WebMvcConfigurer 实现序列化对象是否序列化null和空字符串“”

直接上代码。

此方法为返回前端结果会序列化null字段和空字符、空数组、空map对象等字段

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.ValueFilter;
import com.alibaba.fastjson.support.config.FastJsonConfig;
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.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * webMvc配置类
 *
 * @author demo
 * @date 2021年6月27日
 **/
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private WithSignMessageConverter withSignMessageConverter;

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        // 添加fastjson的配置信息,比如:是否要格式化返回的json数据
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        // 设置序列化特性
        fastJsonConfig.setSerializerFeatures(
                SerializerFeature.PrettyFormat,
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteEnumUsingToString
        );
        // fastjson的值过滤器ValueFilter 可以对序列化的值做统一处理,此处我将为null的空值全部统一处理为空字符串
        ValueFilter valueFilter = (o, s, o1) -> {
            if (null == o1) {
                o1 = "";
            }
            return o1;
        };
        fastJsonConfig.setSerializeFilters(valueFilter);
        // 处理中文乱码问题(不然偶尔会出现中文乱码,我的fastjson版本是1.2.7)
        List<MediaType> fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON);
        withSignMessageConverter.setSupportedMediaTypes(fastMediaTypes);
        // 在convert中添加配置信息
        withSignMessageConverter.setFastJsonConfig(fastJsonConfig);
        converters.add(withSignMessageConverter);
    }
}
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import java.util.List;

/**
 * @author demo
 */
@Component
public class WithSignMessageConverter extends FastJsonHttpMessageConverter {

    @Override
    public List<MediaType> getSupportedMediaTypes(Class<?> clazz) {
        return super.getSupportedMediaTypes(clazz);
    }
}

输出结果:

{
    "age": "",
    "crrq": "",
    "name": "6666",
    "remark": ""
}

此方法为返回前端结果不会序列化null字段和空字符、空数组、空map对象等字段

和上面方法的却别在于 WebConfig 类的 configureMessageConverters 方法的具体实现,去掉了ValueFilter,还有更改了序列化特性方法

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
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.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * webMvc配置类
 *
 * @author demo
 * @date 2021年6月27日
 **/
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private WithSignMessageConverter withSignMessageConverter;

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        // 添加fastjson的配置信息,比如:是否要格式化返回的json数据
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        // 设置序列化特性
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        // 处理中文乱码问题(不然偶尔会出现中文乱码,我的fastjson版本是1.2.7)
        List<MediaType> fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON);
        withSignMessageConverter.setSupportedMediaTypes(fastMediaTypes);
        // 在convert中添加配置信息
        withSignMessageConverter.setFastJsonConfig(fastJsonConfig);
        converters.add(withSignMessageConverter);
    }
}
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import java.util.List;

/**
 * @author demo
 */
@Component
public class WithSignMessageConverter extends FastJsonHttpMessageConverter {

    @Override
    public List<MediaType> getSupportedMediaTypes(Class<?> clazz) {
        return super.getSupportedMediaTypes(clazz);
    }
}

输出结果:

{
    "name": "6666"
}

上一篇:fastjson使用,json ,object,map,list


下一篇:除了闹过腥风血雨的fastjson,你还知道哪些Java解析JSON的利器?