如果你最近也在升级FastJson到FastJson2版本,而跟我一样也遇到了FastJsonHttpMessageConverter找不到类问题以及FastJsonConfig找不到问题,那么恭喜你,看完本文,安装完fastjson2、fastjson2-extension、fastjson2-extension-spring6这三个类库,你就可以成功使用新版FastJson2了。
- @Override
- public void configureMessageConverters(List
> converters) { - converters.clear();
- //FastJsonHttpMessageConverter
- FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
-
- List
fastMediaTypes = new ArrayList<>(); - fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
- fastConverter.setSupportedMediaTypes(fastMediaTypes);
-
- FastJsonConfig fastJsonConfig = new FastJsonConfig();
- fastJsonConfig.setCharset(StandardCharsets.UTF_8);
- fastConverter.setFastJsonConfig(fastJsonConfig);
-
- //StringHttpMessageConverter
- StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();
- stringConverter.setDefaultCharset(StandardCharsets.UTF_8);
- stringConverter.setSupportedMediaTypes(fastMediaTypes);
- converters.add(stringConverter);
- converters.add(fastConverter);
- }
通过官方一个issue中我们发现,他们把fastjson1中的一些类库拆分了FastJsonHttpMessageConverter在2.0.X中不存在的问题 · Issue #168 · alibaba/fastjson2 (github.com)
中我们用的是
- import com.alibaba.fastjson.support.config.FastJsonConfig;
- import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
升级到FastJSON2,我们则需要升级为
- import com.alibaba.fastjson2.support.config.FastJsonConfig;
-
- import com.alibaba.fastjson2.support.spring6.http.converter.FastJsonHttpMessageConverter;
所以你单单引入还不够,我们还需要引入拆分的 fastjson2-extension、fastjson2-extension-spring6 库
- <dependency>
- <groupId>com.alibaba.fastjson2groupId>
- <artifactId>fastjson2artifactId>
- <version>2.0.49version>
- dependency>
- <dependency>
- <groupId>com.alibaba.fastjson2groupId>
- <artifactId>fastjson2-extensionartifactId>
- <version>2.0.49version>
- dependency>
- <dependency>
- <groupId>com.alibaba.fastjson2groupId>
- <artifactId>fastjson2-extension-spring6artifactId>
- <version>2.0.49version>
- dependency>
其中,记得把其他相关的也要升级一下JSON/JSONArray/JSONObject
- import com.alibaba.fastjson2.JSON;
- import com.alibaba.fastjson2.JSONArray;
- import com.alibaba.fastjson2.JSONObject;
(完整版供参考)
- package com.softdev.system.generator.config;
-
-
- // import com.alibaba.fastjson.support.config.FastJsonConfig;
- // import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
- import jakarta.servlet.DispatcherType;
- import org.springframework.boot.web.servlet.FilterRegistrationBean;
- 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.http.converter.StringHttpMessageConverter;
- import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
- import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
-
- import com.alibaba.fastjson2.JSONReader;
- import com.alibaba.fastjson2.JSONWriter;
- import com.alibaba.fastjson2.support.config.FastJsonConfig;
- import com.alibaba.fastjson2.support.spring6.http.converter.FastJsonHttpMessageConverter;
-
- import java.nio.charset.StandardCharsets;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
- /**
- * 2019-2-11 liutf WebMvcConfig 整合 cors 和 SpringMvc MessageConverter
- */
- @Configuration
- public class WebMvcConfig implements WebMvcConfigurer {
-
- @Override
- public void addResourceHandlers(ResourceHandlerRegistry registry) {
- registry.addResourceHandler("/statics/**").addResourceLocations("classpath:/statics/");
- }
- @Bean
- public FilterRegistrationBean xssFilterRegistration() {
- FilterRegistrationBean registration = new FilterRegistrationBean();
- registration.setDispatcherTypes(DispatcherType.REQUEST);
- registration.setFilter(new XssFilter());
- registration.addUrlPatterns("/*");
- registration.setName("xssFilter");
- registration.setOrder(Integer.MAX_VALUE);
- return registration;
- }
-
- // @Override
- // public void configureMessageConverters(List
> converters) { - // converters.clear();
- // //FastJsonHttpMessageConverter
- // FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
-
- // List
fastMediaTypes = new ArrayList<>(); - // fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
- // fastConverter.setSupportedMediaTypes(fastMediaTypes);
-
- // FastJsonConfig fastJsonConfig = new FastJsonConfig();
- // fastJsonConfig.setCharset(StandardCharsets.UTF_8);
- // fastConverter.setFastJsonConfig(fastJsonConfig);
-
- // //StringHttpMessageConverter
- // StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();
- // stringConverter.setDefaultCharset(StandardCharsets.UTF_8);
- // stringConverter.setSupportedMediaTypes(fastMediaTypes);
- // converters.add(stringConverter);
- // converters.add(fastConverter);
- // }
- /**
- * FASTJSON2升级 by https://zhengkai.blog.csdn.net/
- * https://blog.csdn.net/moshowgame/article/details/138013669
- */
- @Override
- public void configureMessageConverters(List
> converters) { - FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
- //自定义配置...
- FastJsonConfig config = new FastJsonConfig();
- config.setDateFormat("yyyy-MM-dd HH:mm:ss");
- config.setReaderFeatures(JSONReader.Feature.FieldBased, JSONReader.Feature.SupportArrayToBean);
- config.setWriterFeatures(JSONWriter.Feature.WriteMapNullValue, JSONWriter.Feature.PrettyFormat);
- converter.setFastJsonConfig(config);
- converter.setDefaultCharset(StandardCharsets.UTF_8);
- converter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));
- converters.add(0, converter);
- }
-
- }