项目场景:
问题描述
Caused by: java.lang.NoSuchMethodError: org.springframework.http.converter.HttpMessageNotReadableException.<init>(Ljava/lang/String;Ljava/lang/Throwable;Lorg/springframework/http/HttpInputMessage;)V
at com.alibaba.fastjson2.support.spring.http.converter.FastJsonHttpMessageConverter.readType(FastJsonHttpMessageConverter.java:113)
at com.alibaba.fastjson2.support.spring.http.converter.FastJsonHttpMessageConverter.read(FastJsonHttpMessageConverter.java:80)
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:201)
原因分析:
解决方案:
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
FastJsonHttpMessageConverter messageConverter = new FastJsonHttpMessageConverter();
messageConverter.setSupportedMediaTypes(Lists.newArrayList(MediaType.APPLICATION_JSON));
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializeFilters(new NullValuePropertyFilter());
fastJsonConfig.setSerializerFeatures(WriteMapNullValue, QuoteFieldNames, DisableCircularReferenceDetect);
messageConverter.setFastJsonConfig(fastJsonConfig);
converters.add(messageConverter);
}
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//在json转换之前先进行string转换, 否则会报错
converters.add(new StringHttpMessageConverter());
MappingJackson2HttpMessageConverter jacksonConverter = new MappingJackson2HttpMessageConverter();
List<MediaType> supportedMediaTypes = Collections.singletonList(MediaType.APPLICATION_JSON);
jacksonConverter.setSupportedMediaTypes(supportedMediaTypes);
ObjectMapper objectMapper = new ObjectMapper();
// 处理循环引用
objectMapper.disable(SerializationFeature.FAIL_ON_SELF_REFERENCES);
// 配置序列化时包含null值
objectMapper.enable(SerializationFeature.WRITE_NULL_MAP_VALUES);
jacksonConverter.setObjectMapper(objectMapper);
converters.add(jacksonConverter);
}
其他:
//fastjson2作为消息转换器
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.support.config.FastJsonConfig;
import com.alibaba.fastjson2.support.spring.http.converter.FastJsonHttpMessageConverter;
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
FastJsonHttpMessageConverter messageConverter = new FastJsonHttpMessageConverter();
messageConverter.setSupportedMediaTypes(Lists.newArrayList(MediaType.APPLICATION_JSON));
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setWriterFilters(new NullValuePropertyFilter());
// fastjson1 的 SerializerFeature.DisableCircularReferenceDetect:
// 在fastjson2 中,代替的是 JSONWriter.Feature.ReferenceDetection,但语义相反,缺省不一样。
// fastjson2 中的 JSONWriter.Feature.ReferenceDetection 缺省是关闭的,而 fastjson1 是默认打开的。
fastJsonConfig.setWriterFeatures(JSONWriter.Feature.WriteNulls);
messageConverter.setFastJsonConfig(fastJsonConfig);
converters.add(messageConverter);
}
fastjson2 官方明确说明 兼容 spring5 和 spring6,未对更早的版本给予说明。