如何解决JSON转换器找不到对应返回类型的问题?

2026-06-10 02:123阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计644个文字,预计阅读时间需要3分钟。

如何解决JSON转换器找不到对应返回类型的问题?

此博客仅为个人学习笔记,汇集了网上各种解决方案和思路,最终实践后添加,作为备忘录使用。错误内容如下:+nested exception is java.lang.IllegalArgumentException: No converter found for value of type [class java.util.Date]


<此博客只为自己学习记录,解决方案是综合了网上各种解决思路和方法,最后实践加以解决后,作为备忘录的用途。>

报错内容如下:

nested exception is java.lang.IllegalArgumentException: No converter found for return value of type: class java.XXX.XXX

直接不废话, 出现这个问题的原因可能包含以下两个:

①springMVC的配置文件

dispatcherServlet-servlet.xml

里面没有对json转换器的配置信息。

如何解决JSON转换器找不到对应返回类型的问题?

②json的三个jar包导入

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.4</version></dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.7.4</version></dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.7.4</version>
</dependency>

①的解决方案,在dispatcherServlet-servlet.xml

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<!--json转换器-->
<ref bean="mappingJacksonHttpMessageConverter" />
</list>
</property>
</bean>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" />
</list>
</property>
</bean>


<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name = "supportedMediaTypes">
<list>
<bean class="org.springframework.http.MediaType">
<constructor-arg index="0" value="text"/>
<constructor-arg index="1" value="plain"/>
<constructor-arg index="2" value="UTF-8"/>
</bean>
<bean class="org.springframework.http.MediaType">
<constructor-arg index="0" value="*"/>
<constructor-arg index="1" value="*"/>
<constructor-arg index="2" value="UTF-8"/>
</bean>
<bean class="org.springframework.http.MediaType">
<constructor-arg index="0" value="text"/>
<constructor-arg index="1" value="*"/>
<constructor-arg index="2" value="UTF-8"/>
</bean>
<bean class="org.springframework.http.MediaType">
<constructor-arg index="0" value="application"/>
<constructor-arg index="1" value="json"/>
<constructor-arg index="2" value="UTF-8"/>
</bean>
</list>
</property> </bean>

②的解决方案: 安装上面提到的jar包,下载导入即可。 maven项目就直接复制粘贴在pom.xml文件里面即可。

本文共计644个文字,预计阅读时间需要3分钟。

如何解决JSON转换器找不到对应返回类型的问题?

此博客仅为个人学习笔记,汇集了网上各种解决方案和思路,最终实践后添加,作为备忘录使用。错误内容如下:+nested exception is java.lang.IllegalArgumentException: No converter found for value of type [class java.util.Date]


<此博客只为自己学习记录,解决方案是综合了网上各种解决思路和方法,最后实践加以解决后,作为备忘录的用途。>

报错内容如下:

nested exception is java.lang.IllegalArgumentException: No converter found for return value of type: class java.XXX.XXX

直接不废话, 出现这个问题的原因可能包含以下两个:

①springMVC的配置文件

dispatcherServlet-servlet.xml

里面没有对json转换器的配置信息。

如何解决JSON转换器找不到对应返回类型的问题?

②json的三个jar包导入

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.4</version></dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.7.4</version></dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.7.4</version>
</dependency>

①的解决方案,在dispatcherServlet-servlet.xml

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<!--json转换器-->
<ref bean="mappingJacksonHttpMessageConverter" />
</list>
</property>
</bean>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" />
</list>
</property>
</bean>


<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name = "supportedMediaTypes">
<list>
<bean class="org.springframework.http.MediaType">
<constructor-arg index="0" value="text"/>
<constructor-arg index="1" value="plain"/>
<constructor-arg index="2" value="UTF-8"/>
</bean>
<bean class="org.springframework.http.MediaType">
<constructor-arg index="0" value="*"/>
<constructor-arg index="1" value="*"/>
<constructor-arg index="2" value="UTF-8"/>
</bean>
<bean class="org.springframework.http.MediaType">
<constructor-arg index="0" value="text"/>
<constructor-arg index="1" value="*"/>
<constructor-arg index="2" value="UTF-8"/>
</bean>
<bean class="org.springframework.http.MediaType">
<constructor-arg index="0" value="application"/>
<constructor-arg index="1" value="json"/>
<constructor-arg index="2" value="UTF-8"/>
</bean>
</list>
</property> </bean>

②的解决方案: 安装上面提到的jar包,下载导入即可。 maven项目就直接复制粘贴在pom.xml文件里面即可。