一、背景
最近閑來無事,想自己搭建一套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>
這樣我們就完美解決了該問題。
四、總結
我們在自己搭建框架的過程中,一定要學會自己多思考,遇到問題多去翻翻源碼,這樣對我們解決問題很有幫助。