• FastJson2中FastJsonHttpMessageConverter找不到类问题


     问题描述 

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

    旧代码

    1. @Override
    2. public void configureMessageConverters(List> converters) {
    3. converters.clear();
    4. //FastJsonHttpMessageConverter
    5. FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
    6. List 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. <dependency>
    2. <groupId>com.alibaba.fastjson2groupId>
    3. <artifactId>fastjson2artifactId>
    4. <version>2.0.49version>
    5. dependency>
    6. <dependency>
    7. <groupId>com.alibaba.fastjson2groupId>
    8. <artifactId>fastjson2-extensionartifactId>
    9. <version>2.0.49version>
    10. dependency>
    11. <dependency>
    12. <groupId>com.alibaba.fastjson2groupId>
    13. <artifactId>fastjson2-extension-spring6artifactId>
    14. <version>2.0.49version>
    15. 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> converters) {
    42. // converters.clear();
    43. // //FastJsonHttpMessageConverter
    44. // FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
    45. // List 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> 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

  • 相关阅读:
    体系结构29_多处理机的互联网络
    C语言网络编程基础(linux)
    java计算机毕业设计古惠农产品线上销售系统MyBatis+系统+LW文档+源码+调试部署
    JavaScript执行机制
    Rhino Linux:滚动发布但也很稳定的 Ubuntu
    Taurus .Net Core 微服务开源框架:Admin 插件【1】 - 微服务节点管理
    Apache Impala 4.1概览
    Android 9.0系统源码_SystemUI(三)系统状态图标控制
    如何开启Linux的SSH服务?sudo service ssh start和sudo systemctl enable ssh两种不同的开启方式有什么区别?
    计算机的 bit(比特)和Byte(字节)
  • 原文地址:https://blog.csdn.net/moshowgame/article/details/138013669