FastJson2中FastJsonHttpMessageConverter找不到类问题

 问题描述 

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

旧代码

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.clear();
        //FastJsonHttpMessageConverter
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();

        List<MediaType> 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);
    }

 maven build error

 RCA调查

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

 FastJSON1包

中我们用的是

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

FastJSON2包

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

import com.alibaba.fastjson2.support.config.FastJsonConfig;

import com.alibaba.fastjson2.support.spring6.http.converter.FastJsonHttpMessageConverter;

解决方法 

pom/xml引入完整类库

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

<!--  FastJson2中FastJsonHttpMessageConverter找不到类问题 by https://zhengkai.blog.csdn.net/-->
<!-- https://mvnrepository.com/artifact/com.alibaba.fastjson2/fastjson2 -->
<dependency>
	<groupId>com.alibaba.fastjson2</groupId>
	<artifactId>fastjson2</artifactId>
	<version>2.0.49</version>
</dependency>
<dependency>
    <groupId>com.alibaba.fastjson2</groupId>
    <artifactId>fastjson2-extension</artifactId>
    <version>2.0.49</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba.fastjson2/fastjson2-extension-spring6 -->
<dependency>
	<groupId>com.alibaba.fastjson2</groupId>
	<artifactId>fastjson2-extension-spring6</artifactId>
	<version>2.0.49</version>
</dependency>

WebMvcConfig和configureMessageConverters配置

 其中,记得把其他相关的也要升级一下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<HttpMessageConverter<?>> converters) {
    //     converters.clear();
    //     //FastJsonHttpMessageConverter
    //     FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();

    //     List<MediaType> 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<HttpMessageConverter<?>> 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);
    }

}

 MVN build successfully

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Fastjson是一个用于处理JSON数据的Java库,Fastjson2则是Fastjson的第二个版本。 Fastjson2提供了一个FastJsonHttpMessageConverter,用于在Spring MVC将Java对象转换为JSON数据。它可以将请求的JSON数据转换为Java对象,并将Java对象转换为响应的JSON数据。这个转换器可以方便地集成到Spring应用程序。 要在Spring MVC使用FastJsonHttpMessageConverter,你需要在配置文件进行相应的配置。首先,你需要添加Fastjson的依赖项到你的项目。然后,在Spring的配置文件,通过添加以下代码来配置FastJsonHttpMessageConverter: ```java @Configuration public class WebMvcConfig extends WebMvcConfigurerAdapter { @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter(); converters.add(fastConverter); } @Override public void extendMessageConverters(List<HttpMessageConverter<?>> converters) { for (HttpMessageConverter<?> converter : converters) { if (converter instanceof MappingJackson2HttpMessageConverter) { MappingJackson2HttpMessageConverter jsonConverter = (MappingJackson2HttpMessageConverter) converter; jsonConverter.setObjectMapper(new ObjectMapper()); } } } } ``` 以上配置将FastJsonHttpMessageConverter添加到Spring MVC,并将其作为默认的JSON转换器。你还可以根据需要进一步配置Fastjson的相关选项,比如日期格式化型处理等。 希望这能解答你关于Fastjson2和FastJsonHttpMessageConverter问题!如有更多疑问,请随时提出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值