解决java.lang.IllegalArgumentException: No converter found for return value of type: class java.util.ArrayList问题

在spring + springmvc + mybatis框架中,我们配置接口对外返回json格式,但是报如下错误:

 1 24-Oct-2017 17:42:23.495 严重 [http-nio-8080-exec-6] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IllegalArgumentException: No converter found for return value of type: class java.util.ArrayList] with root cause
 2  java.lang.IllegalArgumentException: No converter found for return value of type: class java.util.ArrayList
 3     at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:187)
 4     at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:174)
 5     at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:81)
 6     at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:113)
 7     at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
 8     at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
 9     at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
10     at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
11     at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
12     at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
13     at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
14     at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
15     at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
16     at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
17     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230)
18     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
19     at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
20     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
21     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
22     at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
23     at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
24     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
25     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
26     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
27     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:108)
28     at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
29     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
30     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
31     at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
32     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
33     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)
34     at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:784)
35     at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
36     at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:802)
37     at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1410)
38     at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
39     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
40     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
41     at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
42     at java.lang.Thread.run(Thread.java:745)

经过度娘,因为springmvc默认是没有对象转换成json的转换器的,需要手动添加jackson依赖。

因此,在build.gradle中加入:

1     compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.8.8'
2     // https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind
3     compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.8.8'
4 // https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations
5     compile group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.8.8'

如果maven,加入:

手动添加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>

感谢地址为:http://www.cnblogs.com/hafiz/p/5812873.html的博主。

    原文作者:飞鱼君
    原文地址: https://www.cnblogs.com/feiyujun/p/7725349.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞