解决java.lang.IllegalArgumentException: No converter found for return value of type 的问题

controller返回一个dto,并且定义了@ResponseBody注解,表示希望将这个dto对象转换为json字符串返回给前端,但是运行时报错:nested exception is java.lang.IllegalArgumentException: No converter found for return value of type:XXX.XXX.dto。

这是因为springmvc默认是没有对象转换成json的转换器的,需要手动添加jackson依赖。

解决方法为手动添加jackson依赖到pom.xml文件中:

<properties>
    <jackson.version>2.5.4</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>
原文地址:https://www.cnblogs.com/laoxia/p/10247557.html