解決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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM