No converter found for return value of type: class java.util.ArrayList
這個問題困擾了許久,終於解決了。
先說下專案環境,因為不同的版本,配置會略有不同,下面稍有提及。
專案環境:jdk1.8 + spring4.3.5 + jackson2.7.4
方法報404錯誤,檢視控制檯輸出,發現報錯資訊java.lang.IllegalArgumentException: No converter found for return value of type: class java.util.ArrayList,多測試幾次,發現只能轉換String型別,而Map,List集合類轉換異常。
/*** * 根據id查詢下級 * * @return */ @ResponseBody @RequestMapping("/getSubList/{organId}/{status}") public List<Organ> getSubList(@PathVariable String organId, @PathVariable String status, HttpServletRequest req) { OrganQuery query = new OrganQuery(); query.setSuperId(organId); query.setValidStatus(status); query.setPagesize(20); List<Organ> list = this.organService.queryOrganList(query); return list; }
資訊說是json轉換失敗,首先確認jackson依賴已經匯入了(注意版本衝突問題,比如spring4.x 至少要用Jackson2.6以上)
<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-databind</artifactId> <version>2.7.4</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.7.4</version> </dependency>
然後檢視spring-mvc.xml配置檔案是否配置了SpringMVC前端 JSON轉換器 MappingJackson2HttpMessageConverter以及StringHttpMessageConverter處理中文亂碼問題
<!-- 該配置版本必須spring4.x 和 jackson2.x以上 --> <!-- 配置處理介面卡 Controller類裡面@responseBody方法的返回值都需要經過訊息轉換器--> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="messageConverters"> <list> <!-- 處理解決@ResponseBody返回中文亂碼情況 --> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> <value>text/plain;charset=UTF-8</value> <value>application/json;charset=UTF-8</value> </list> </property> </bean> <!-- 處理解決後臺list、map轉換json資料問題 --> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" /> </list> </property> <!-- 日期格式轉換 自定義類繼承 webBindingInitializer介面--> <property name="webBindingInitializer"> <bean class="com.qly.b2b.web.common.DateConverter" /> </property> </bean> <!-- 啟動Spring MVC的註解功能,完成請求和註解POJO的對映 --> <mvc:annotation-driven/>
注意: <mvc:annotation-driven/>必須在RequestMappingHandlerAdapter介面卡之後,否則無效,且可能其他請求方法返回的資料出現亂碼情況(對單一請求方法的解決可以通過在controller層的對應的requestMapping加上produces={“application/json;charset=UTF-8”}屬性,而全域性方法最好是如上面一樣配置converter.StringHttpMessageConverter轉換器)
其他的一些配置注意點,
spring3.1.2以下版本引用的是org.springframework.http.converter.json.MappingJacksonHttpMessageConverter類,
spring3.1.2以上的版本引用的是org.springframework.http.converter.json.MappingJackson2HttpMessageConverter,多了一個2。
版本還是得用新的好。
附完整的spring-mvc.xml配置檔案:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 開啟註解 -->
<context:annotation-config/>
<!-- 把標記了@Controller註解的類轉換為bean -->
<context:component-scan base-package="com.xxx.controller" />
<!-- 切面 開啟對@AspectJ註解的支援 -->
<!-- 通知spring使用cglib而不是jdk的來生成代理方法 AOP可以攔截到Controller-->
<aop:aspectj-autoproxy proxy-target-class="true"/>
<!-- 使用預設的servlet來相應靜態檔案 -->
<mvc:default-servlet-handler />
<!-- 對模型檢視名稱的解析,即在模型檢視名稱新增前後綴 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/view/" />
<property name="suffix" value=".jsp" />
</bean>
<mvc:view-controller path="/" view-name="login/index"/>
<!-- 配置處理介面卡 Controller類裡面@responseBody方法的返回值都需要經過訊息轉換器-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<!-- 處理解決@ResponseBody返回中文亂碼情況 -->
<bean
class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>text/plain;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- 處理解決後臺list、map轉換json資料問題 -->
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
</list>
</property>
<!-- 日期格式轉換 自定義類繼承 webBindingInitializer介面-->
<property name="webBindingInitializer">
<bean class="com.xxx.web.common.DateConverter" />
</property>
</bean>
<!-- 啟動Spring MVC的註解功能,完成請求和註解POJO的對映 -->
<mvc:annotation-driven/>
</beans>