配置

  1. import com.alibaba.fastjson.serializer.SerializerFeature;
  2. import com.alibaba.fastjson.support.config.FastJsonConfig;
  3. import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.http.MediaType;
  7. import org.springframework.http.converter.HttpMessageConverter;
  8. import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
  9. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. @Configuration
  13. public class MvcConfig implements WebMvcConfigurer {
  14. @Autowired
  15. private UserInterceptor userInterceptor;
  16. private final String[] excludes = new String[]{"/static/**", "/webjars/**", "/doc.html/**", "/swagger-resources/**",
  17. "/error"};
  18. @Override
  19. public void addInterceptors(InterceptorRegistry registry) {
  20. registry.addInterceptor(userInterceptor)
  21. .addPathPatterns("/**")
  22. .excludePathPatterns(excludes);
  23. }
  24. @Override
  25. public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
  26. //1.需要定义一个convert转换消息的对象;
  27. FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
  28. //2.添加fastJson的配置信息,比如:是否要格式化返回的json数据;
  29. FastJsonConfig fastJsonConfig = new FastJsonConfig();
  30. fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat,
  31. SerializerFeature.WriteMapNullValue,
  32. SerializerFeature.WriteNullStringAsEmpty,
  33. SerializerFeature.WriteNullListAsEmpty);
  34. //3处理中文乱码问题
  35. List<MediaType> fastMediaTypes = new ArrayList<>();
  36. fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
  37. //4.在convert中添加配置信息.
  38. fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
  39. fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
  40. //5.将convert添加到converters当中.
  41. converters.add(0, fastJsonHttpMessageConverter); /**注意springboot1.x不需要先加个0*/
  42. }
  43. }

fastjson SerializerFeature说明

  1. QuoteFieldNames 输出key时是否使用双引号,默认为true
  2. UseSingleQuotes 使用单引号而不是双引号,默认为false
  3. WriteMapNullValue 是否输出值为null的字段,默认为false
  4. WriteEnumUsingToString Enum输出name()或者original,默认为false
  5. UseISO8601DateFormat Date使用ISO8601格式输出,默认为false
  6. WriteNullListAsEmpty List字段如果为null,输出为[],而非null
  7. WriteNullStringAsEmpty 字符类型字段如果为null,输出为”“,而非null
  8. WriteNullNumberAsZero 数值字段如果为null,输出为0,而非null
  9. WriteNullBooleanAsFalse Boolean字段如果为null,输出为false,而非null
  10. SkipTransientField 如果是true,类中的Get方法对应的Fieldtransient,序列化时将会被忽略。默认为true
  11. SortField 按字段名称排序后输出。默认为false
  12. WriteTabAsSpecial \t做转义输出,默认为false 不推荐
  13. PrettyFormat 结果是否格式化,默认为false
  14. WriteClassName 序列化时写入类型信息,默认为false。反序列化是需用到
  15. DisableCircularReferenceDetect 消除对同一对象循环引用的问题,默认为false
  16. WriteSlashAsSpecial 对斜杠’/’进行转义
  17. BrowserCompatible 将中文都会序列化为\uXXXX格式,字节数会多一些,但是能兼容IE 6,默认为false
  18. WriteDateUseDateFormat 全局修改日期格式,默认为falseJSON.DEFFAULT_DATE_FORMAT = yyyy-MM-dd”;JSON.toJSONString(obj, SerializerFeature.WriteDateUseDateFormat);
  19. DisableCheckSpecialChar 一个对象的字符串属性中如果有特殊字符如双引号,将会在转成json时带有反斜杠转移符。如果不需要转义,可以使用这个属性。默认为false
  20. NotWriteRootClassName 含义
  21. BeanToArray 将对象转为array输出
  22. WriteNonStringKeyAsString 含义
  23. NotWriteDefaultValue 含义
  24. BrowserSecure 含义
  25. IgnoreNonFieldGetter 含义
  26. WriteEnumUsingName 含义