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

Posted 飞鱼君

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了解决java.lang.IllegalArgumentException: No converter found for return value of type: class java.util.A相关的知识,希望对你有一定的参考价值。

在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的博主。

以上是关于解决java.lang.IllegalArgumentException: No converter found for return value of type: class java.util.A的主要内容,如果未能解决你的问题,请参考以下文章

云原生景观:可观察性和分析解决了什么问题?如何解决的?

vs2008解决方案资源管理器不显示解决方案

什么是 沙盒解决方案

解决PowerShell乱码问题

如何解决解决方案中现有项目的c#CS0006错误

关于问题解决与事件解决的思考