当前位置:   article > 正文

FastJson2中FastJsonHttpMessageConverter找不到类问题_fastjson2-extension-spring6

fastjson2-extension-spring6

 问题描述 

如果你最近也在升级FastJson到FastJson2版本,而跟我一样也遇到了FastJsonHttpMessageConverter找不到类问题以及FastJsonConfig找不到问题,那么恭喜你,看完本文,安装完fastjson2、fastjson2-extension、fastjson2-extension-spring6这三个类库,你就可以成功使用新版FastJson2了。

旧代码

  1. @Override
  2. public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
  3. converters.clear();
  4. //FastJsonHttpMessageConverter
  5. FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
  6. List<MediaType> fastMediaTypes = new ArrayList<>();
  7. fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
  8. fastConverter.setSupportedMediaTypes(fastMediaTypes);
  9. FastJsonConfig fastJsonConfig = new FastJsonConfig();
  10. fastJsonConfig.setCharset(StandardCharsets.UTF_8);
  11. fastConverter.setFastJsonConfig(fastJsonConfig);
  12. //StringHttpMessageConverter
  13. StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();
  14. stringConverter.setDefaultCharset(StandardCharsets.UTF_8);
  15. stringConverter.setSupportedMediaTypes(fastMediaTypes);
  16. converters.add(stringConverter);
  17. converters.add(fastConverter);
  18. }

 maven build error

 RCA调查

通过官方一个issue中我们发现,他们把fastjson1中的一些类库拆分了FastJsonHttpMessageConverter在2.0.X中不存在的问题 · Issue #168 · alibaba/fastjson2 (github.com)

 FastJSON1包

中我们用的是

  1. import com.alibaba.fastjson.support.config.FastJsonConfig;
  2. import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

FastJSON2包

升级到FastJSON2,我们则需要升级为

  1. import com.alibaba.fastjson2.support.config.FastJsonConfig;
  2. import com.alibaba.fastjson2.support.spring6.http.converter.FastJsonHttpMessageConverter;

解决方法 

pom/xml引入完整类库

所以你单单引入还不够,我们还需要引入拆分的 fastjson2-extensionfastjson2-extension-spring6

  1. <!-- FastJson2中FastJsonHttpMessageConverter找不到类问题 by https://zhengkai.blog.csdn.net/-->
  2. <!-- https://mvnrepository.com/artifact/com.alibaba.fastjson2/fastjson2 -->
  3. <dependency>
  4. <groupId>com.alibaba.fastjson2</groupId>
  5. <artifactId>fastjson2</artifactId>
  6. <version>2.0.49</version>
  7. </dependency>
  8. <dependency>
  9. <groupId>com.alibaba.fastjson2</groupId>
  10. <artifactId>fastjson2-extension</artifactId>
  11. <version>2.0.49</version>
  12. </dependency>
  13. <!-- https://mvnrepository.com/artifact/com.alibaba.fastjson2/fastjson2-extension-spring6 -->
  14. <dependency>
  15. <groupId>com.alibaba.fastjson2</groupId>
  16. <artifactId>fastjson2-extension-spring6</artifactId>
  17. <version>2.0.49</version>
  18. </dependency>

WebMvcConfig和configureMessageConverters配置

 其中,记得把其他相关的也要升级一下JSON/JSONArray/JSONObject

  1. import com.alibaba.fastjson2.JSON;
  2. import com.alibaba.fastjson2.JSONArray;
  3. import com.alibaba.fastjson2.JSONObject;

(完整版供参考)

  1. package com.softdev.system.generator.config;
  2. // import com.alibaba.fastjson.support.config.FastJsonConfig;
  3. // import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
  4. import jakarta.servlet.DispatcherType;
  5. import org.springframework.boot.web.servlet.FilterRegistrationBean;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.context.annotation.Configuration;
  8. import org.springframework.http.MediaType;
  9. import org.springframework.http.converter.HttpMessageConverter;
  10. import org.springframework.http.converter.StringHttpMessageConverter;
  11. import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
  12. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  13. import com.alibaba.fastjson2.JSONReader;
  14. import com.alibaba.fastjson2.JSONWriter;
  15. import com.alibaba.fastjson2.support.config.FastJsonConfig;
  16. import com.alibaba.fastjson2.support.spring6.http.converter.FastJsonHttpMessageConverter;
  17. import java.nio.charset.StandardCharsets;
  18. import java.util.ArrayList;
  19. import java.util.Collections;
  20. import java.util.List;
  21. /**
  22. * 2019-2-11 liutf WebMvcConfig 整合 cors 和 SpringMvc MessageConverter
  23. */
  24. @Configuration
  25. public class WebMvcConfig implements WebMvcConfigurer {
  26. @Override
  27. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  28. registry.addResourceHandler("/statics/**").addResourceLocations("classpath:/statics/");
  29. }
  30. @Bean
  31. public FilterRegistrationBean xssFilterRegistration() {
  32. FilterRegistrationBean registration = new FilterRegistrationBean();
  33. registration.setDispatcherTypes(DispatcherType.REQUEST);
  34. registration.setFilter(new XssFilter());
  35. registration.addUrlPatterns("/*");
  36. registration.setName("xssFilter");
  37. registration.setOrder(Integer.MAX_VALUE);
  38. return registration;
  39. }
  40. // @Override
  41. // public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
  42. // converters.clear();
  43. // //FastJsonHttpMessageConverter
  44. // FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
  45. // List<MediaType> fastMediaTypes = new ArrayList<>();
  46. // fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
  47. // fastConverter.setSupportedMediaTypes(fastMediaTypes);
  48. // FastJsonConfig fastJsonConfig = new FastJsonConfig();
  49. // fastJsonConfig.setCharset(StandardCharsets.UTF_8);
  50. // fastConverter.setFastJsonConfig(fastJsonConfig);
  51. // //StringHttpMessageConverter
  52. // StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();
  53. // stringConverter.setDefaultCharset(StandardCharsets.UTF_8);
  54. // stringConverter.setSupportedMediaTypes(fastMediaTypes);
  55. // converters.add(stringConverter);
  56. // converters.add(fastConverter);
  57. // }
  58. /**
  59. * FASTJSON2升级 by https://zhengkai.blog.csdn.net/
  60. * https://blog.csdn.net/moshowgame/article/details/138013669
  61. */
  62. @Override
  63. public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
  64. FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
  65. //自定义配置...
  66. FastJsonConfig config = new FastJsonConfig();
  67. config.setDateFormat("yyyy-MM-dd HH:mm:ss");
  68. config.setReaderFeatures(JSONReader.Feature.FieldBased, JSONReader.Feature.SupportArrayToBean);
  69. config.setWriterFeatures(JSONWriter.Feature.WriteMapNullValue, JSONWriter.Feature.PrettyFormat);
  70. converter.setFastJsonConfig(config);
  71. converter.setDefaultCharset(StandardCharsets.UTF_8);
  72. converter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));
  73. converters.add(0, converter);
  74. }
  75. }

 MVN build successfully

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Li_阴宅/article/detail/921839
推荐阅读
相关标签
  

闽ICP备14008679号