天天看点

关于json转换器缺失的问题,报错内容:No converter found for return value of type

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

报错内容如下:

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

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

①springMVC的配置文件

dispatcherServlet-servlet.xml

里面没有对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>