zoukankan      html  css  js  c++  java
  • springboot 利用configureMessageConverters add FastJsonHttpMessageConverter 实现返回JSON值 null to ""

    /*
    *
    * 文件名:@WebConfiguration.java <br/>
    * @author tomas <br/>

    import com.alibaba.fastjson.support.config.FastJsonConfig;
    import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.boot.actuate.health.ApplicationHealthIndicator;
    import org.springframework.boot.actuate.health.HealthIndicator;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
    import org.springframework.context.EnvironmentAware;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.core.env.Environment;
    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.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

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

    import static com.alibaba.fastjson.serializer.SerializerFeature.*;

    /**
    * 类名:WebConfiguration <br />
    *
    * 功能:Web相关配置
    *
    * @author tomas <br />
    * 创建时间:2016年7月27日 下午3:57:19 <br />
    * @version 2016年7月27日*/
    @Configuration
    @EnableWebMvc
    public class FrontConfiguration extends WebMvcConfigurerAdapter implements EnvironmentAware {
    // 日志记录器
    private static final Logger logger = LoggerFactory.getLogger(FrontConfiguration.class);

    // 当前的环境对象
    protected Environment environment;


    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    super.configureMessageConverters(converters);
    //1.需要先定义一个 convert 转换消息的对象;
    FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();

    //2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据;
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    // 不忽略对象属性中的null值
    fastJsonConfig.setSerializerFeatures(
    PrettyFormat,
    WriteNullListAsEmpty,
    WriteNullStringAsEmpty);
    //3、在convert中添加配置信息.
    fastConverter.setFastJsonConfig(fastJsonConfig);
    //4、将convert添加到converters当中.
    converters.add(fastConverter);
    }


    public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("swagger-ui.html")
    .addResourceLocations("classpath:/META-INF/resources/");
    registry.addResourceHandler("favicon.ico")
    .addResourceLocations("classpath:/static/favicon.ico");
    registry.addResourceHandler("/webjars/**")
    .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
    /**
    * Set the {@code Environment} that this object runs in.
    *
    * @param environment
    */
    @Override
    public void setEnvironment(Environment environment) {
    this.environment = environment;
    }
    }
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    super.configureMessageConverters(converters);
    //1.需要先定义一个 convert 转换消息的对象;
    FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
    List<MediaType> supportedMediaTypes = new ArrayList<>();
    supportedMediaTypes.add(MediaType.APPLICATION_JSON);
    supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
    supportedMediaTypes.add(MediaType.APPLICATION_ATOM_XML);
    supportedMediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
    supportedMediaTypes.add(MediaType.APPLICATION_OCTET_STREAM);
    supportedMediaTypes.add(MediaType.APPLICATION_PDF);
    supportedMediaTypes.add(MediaType.APPLICATION_RSS_XML);
    supportedMediaTypes.add(MediaType.APPLICATION_XHTML_XML);
    supportedMediaTypes.add(MediaType.APPLICATION_XML);
    supportedMediaTypes.add(MediaType.IMAGE_GIF);
    supportedMediaTypes.add(MediaType.IMAGE_JPEG);
    supportedMediaTypes.add(MediaType.IMAGE_PNG);
    supportedMediaTypes.add(MediaType.TEXT_EVENT_STREAM);
    supportedMediaTypes.add(MediaType.TEXT_HTML);
    supportedMediaTypes.add(MediaType.TEXT_MARKDOWN);
    supportedMediaTypes.add(MediaType.TEXT_PLAIN);
    supportedMediaTypes.add(MediaType.TEXT_XML);
    fastConverter.setSupportedMediaTypes(supportedMediaTypes);
    //2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据;
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    // 不忽略对象属性中的null值
    fastJsonConfig.setSerializerFeatures(
    PrettyFormat,
    WriteNullListAsEmpty,
    WriteNullStringAsEmpty);
    //3、在convert中添加配置信息.
    fastConverter.setFastJsonConfig(fastJsonConfig);
    //4、将convert添加到converters当中.
    converters.add(fastConverter);
    }
  • 相关阅读:
    Mysql 数据库优化(五)sql语句优化【个人经验】转
    C# 编写的winform程序的托盘功能
    Mysql 数据库‘’函数‘、‘’事件‘’完成时间字段自动更新
    dd
    c# ‘using’三种用法
    集群、分布式、负载均衡
    iOS开发- Xcode 7添加pch文件
    iOS开发-pod install 出错 The dependency `AFNetworking (~> 2.6.0)` is not used in any concrete target.
    leetcode -- Find Median from Data Stream
    ubuntu14.04 LTS 更新源
  • 原文地址:https://www.cnblogs.com/xmanblue/p/7779479.html
Copyright © 2011-2022 走看看