SpringMVC 报错HTTP Status 500 - No converter found for return value of type解决方案
程序员文章站
2022-06-28 18:13:06
报错分析:一、背景 最近由于项目需要,搭建一套Spring+SpringMVC+Mybatis+Mysql的环境,结果在使用postmen访问接口时候,接口@ResponseBody注解进行返回Response封装类报错:HTTPStatus500-Noconverterfoundforreturnvalueoftype:classmicroplay.config.response.MPResponse错误。二、框架搭建环境 1.jdk 1.8 2.m......
报错分析:
一、背景
最近由于项目需要,搭建一套Spring+SpringMVC+Mybatis+Mysql的环境,结果在使用postmen访问接口时候,接口@ResponseBody注解进行返回Response封装类报错:
HTTP Status 500 - No converter found for return value of type: class microplay.config.response.MPResponse错误。
二、框架搭建环境
1.jdk 1.8
2.maven 3.5.2
3.spring 5.2.9.RELEASE
4.springmvc 5.2.9.RELEASE
三、错误原因及解决步骤
1.原因:这是因为springmvc默认是没有对象转换成json的转换器的,需要手动添加jackson依赖。
2.解决步骤:
手动添加jackson依赖到pom.xml文件中
<properties>
<jackson.version>2.9.8</jackson.version>
</properties>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
如果还是没有解决,则进行以下步骤:在springmvc配置文件中进行如下配置
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>
这样我们就完美解决了该问题。
但是,本人项目需要使用
org.springframework.web.reactive.function.client.WebClient
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webflux -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webflux</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<!--<!– https://mvnrepository.com/artifact/io.projectreactor.netty/reactor-netty –>-->
<dependency>
<groupId>io.projectreactor.netty</groupId>
<artifactId>reactor-netty</artifactId>
<version>0.9.7.RELEASE</version>
</dependency>
在使用WebClient访问第三方接口时候,由于jackson-databin包的冲突,webclient访问第三方网络接口报各种报错:
Some Error Happend :org.springframework.web.reactive.function.client.WebClientResponseException$InternalServerError: 500 Internal Server Error from POST https://
org.springframework.web.reactive.function.client.WebClientResponseException$InternalServerError: 500 Internal Server Error from POST https://
at org.springframework.web.reactive.function.client.WebClientResponseException.create(WebClientResponseException.java:201)
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Error has been observed at the following site(s):
|_ checkpoint ? 500 from POST https:// [DefaultWebClient]
Stack trace:
at org.springframework.web.reactive.function.client.WebClientResponseException.create(WebClientResponseException.java:201)
at org.springframework.web.reactive.function.client.DefaultClientResponse.lambda$createException$1(DefaultClientResponse.java:209)
...
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.lang.Thread.run(Thread.java:748)
Some Error Happend :org.springframework.web.reactive.function.client.WebClientResponseException$InternalServerError: 500 Internal Server Error from POST https://
因为是jackson-databin包的冲突的原因:
- 注销该包,但是注销后,报错- No converter found for return value of type;
- 使用其他版本的jackson-databin包,曾经一一尝试过其他版本的jackson-databin包,但是依然没法解决;
- Spring MVC4设置使用fastjson作为json解析器,替代jackson;(该方法完美解决)
Spring MVC4设置使用fastjson作为json解析器,替代jackson
1、添加Fastjson的包,如果使用Maven,可使用如下设置:
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.73</version>
</dependency>
2、在pom.xml中注释掉jackson-databin包,在spring-servler.xml(SpringMVC的配置文件)中将Jackson的配置去掉,换上下面的配置:
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes" value="application/json"/>
<!--设置fastjson特性-->
<property name="features">
<array>
<!--设置null值也要输出,fastjson默认是关闭的-->
<value>WriteMapNullValue</value>
<!--设置使用文本方式输出日期,fastjson默认是long-->
<value>WriteDateUseDateFormat</value>
</array>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
替换以后完美解决jackson-databin包的冲突问题。
本文地址:https://blog.csdn.net/yang5726685/article/details/110956138