解決java.lang.IllegalArgumentException: No converter found for return
阿新 • • 發佈:2018-11-10
一、背景
最近閒來無事,想自己搭建一套Spring+SpringMVC+Mybatis+Mysql的環境(搭建步驟會在以後部落格中給出),結果執行程式時,適用@ResponseBody註解進行返回List<物件>的json資料時出現了:nested exception is java.lang.IllegalArgumentException: No converter found for return value of type: class java.util.ArrayList錯誤,就細細分析了下,而後解決了該問題,先拿來備份和分享!
二、框架搭建環境
1.jdk 1.7
2.maven 3.3.9
3.spring 4.2.6.RELEASE
4.springmvc 4.2.6.RELEASE
5.mybatis 3.2.8
三、錯誤原因及解決步驟
1.原因:這是因為springmvc預設是沒有物件轉換成json的轉換器的,需要手動新增jackson依賴。
2.解決步驟:
手動新增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>
這樣我們就完美解決了該問題。
四、總結
我們在自己搭建框架的過程中,一定要學會自己多思考,遇到問題多去翻翻原始碼,這樣對我們解決問題很有幫助。