springboot 处理返回结果中字段为空或为null,不展示字段的问题(字段展示不全)

时间:2025-03-17 07:05:21
package ;

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

import ;
import ;
import ;

/**
 * description: fastjson处理返回的参数为null、或者不返回
 * date: 2019/11/22 15:03
 * author: hantao
 * version: 1.0
 * springboot 处理返回结果中字段为空或为null,不展示字段的问题(字段展示不全)
 */
@Configuration
public class FastJsonConfiguration extends WebMvcConfigurationSupport {

    /**
     * 使用阿里 fastjson 作为JSON MessageConverter
     * @param converters
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        FastJsonConfig config = new FastJsonConfig();
        (
                // 保留map空的字段
                ,
                // 将String类型的null转成""
                ,
                // 将Number类型的null转成0
                ,
                // 将List类型的null转成[]
                ,
                // 将Boolean类型的null转成false
                ,
                // 避免循环引用
                );

        (config);
        (("UTF-8"));
        List<MediaType> mediaTypeList = new ArrayList<>();
        // 解决中文乱码问题,相当于在Controller上的@RequestMapping中加了个属性produces = "application/json"
        (MediaType.APPLICATION_JSON);
        (mediaTypeList);
        (converter);
    }

    /**
     * 整合了swagger需要配置swagger拦截
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        ("","").addResourceLocations("classpath:/META-INF/resources/");
        ("").addResourceLocations("classpath:/META-INF/resources/");
        ("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        ("/static/**").addResourceLocations("classpath:/META-INF/resources/static/");
    }

}