content: package com.kefu.common.config;
  
  import com.alibaba.fastjson.serializer.SerializerFeature;
  import com.alibaba.fastjson.support.config.FastJsonConfig;
  import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
  
  import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.http.MediaType;
  import org.springframework.http.converter.HttpMessageConverter;
  import org.springframework.web.cors.CorsConfiguration;
  import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
  import org.springframework.web.filter.CorsFilter;
  
  import java.util.ArrayList;
  import java.util.List;
  
  /**
   * 一些全局配置
   *
   * @author feng
   * @date 2019-05-20
   */
  @Configuration
  public class GlobalConfig {
  
      /**
       * 配置请求跨域
       *
       * @return
       */
      @Bean
      public CorsFilter corsFilter() {
          final UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
          final CorsConfiguration cors = new CorsConfiguration();
          cors.setAllowCredentials(true);
          cors.addAllowedOrigin("*");
          cors.addAllowedHeader("*");
          cors.addAllowedMethod("*");
          urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", cors);
          return new CorsFilter(urlBasedCorsConfigurationSource);
      }
  
      /**
       * 用FastJson替换默认的Jackson作为HTTP消息转换器
       *
       * @return
       */
      @Bean
      public HttpMessageConverters httpMessageConverters() {
          // 1.需要定义一个convert转换消息的对象;
          FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
          // 2.添加fastJson的配置信息;
          FastJsonConfig fastJsonConfig = new FastJsonConfig();
          fastJsonConfig.setSerializerFeatures(
                  // 格式化JSON
                  SerializerFeature.PrettyFormat,
                  // 是否输出值为null的字段,默认为false
                  SerializerFeature.WriteMapNullValue,
                  // 字符类型字段如果为null,输出为"",而非null
                  SerializerFeature.WriteNullStringAsEmpty,
                  // List字段如果为null,输出为[],而非null
                  SerializerFeature.WriteNullListAsEmpty,
                  // 消除循环引用
                  SerializerFeature.DisableCircularReferenceDetect);
          // 3.处理中文乱码问题
          List<MediaType> fastMediaTypes = new ArrayList<>();
          fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
          // 4.在convert中添加配置信息.
          fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
          fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
          return new HttpMessageConverters((HttpMessageConverter<?>) fastJsonHttpMessageConverter);
      }
  }
  