🚫 不推荐使用 Fastjson,可使用 Jackson 自行封装静态类进行调用,可参考:https://github.com/anydong/example-springboot/blob/main/utils/src/main/java/com/example/demo/utils/JsonUtils.java 实现

pom.xml

  1. <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
  2. <dependency>
  3. <groupId>com.alibaba</groupId>
  4. <artifactId>fastjson</artifactId>
  5. <version>1.2.56</version>
  6. </dependency>

配置

  1. package com.example.api.config;
  2. import com.alibaba.fastjson.serializer.SerializerFeature;
  3. import com.alibaba.fastjson.support.config.FastJsonConfig;
  4. import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.http.converter.HttpMessageConverter;
  7. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  8. import java.nio.charset.StandardCharsets;
  9. import java.util.List;
  10. /**
  11. * WebMvcConfigurerImpl
  12. *
  13. * @author Where
  14. * @date 2019-01-31
  15. */
  16. @Configuration
  17. public class WebMvcConfigurerImpl implements WebMvcConfigurer {
  18. @Override
  19. public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
  20. FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
  21. FastJsonConfig config = new FastJsonConfig();
  22. config.setSerializerFeatures(
  23. SerializerFeature.WriteNullListAsEmpty,
  24. SerializerFeature.WriteMapNullValue,
  25. SerializerFeature.UseISO8601DateFormat,
  26. SerializerFeature.DisableCircularReferenceDetect
  27. );
  28. config.setCharset(StandardCharsets.UTF_8);
  29. converter.setFastJsonConfig(config);
  30. converters.add(0, converter);
  31. }
  32. }

参考文献