这个问题困扰了许久,终于解决了。
先说下项目环境,因为不同的版本,配置会略有不同,下面稍有提及。
项目环境: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>