0

I am returning a simple list object using spring controller. I have annotated correct with @ResponseBody to my method, but still its not able to convert from arraylist. I have added jackson-mapper-asl jar too.

My code of controller is

@Controller
@RequestMapping("/service/greeting")
public class SpringServiceController {


    @RequestMapping(value = "/customers", method = RequestMethod.GET)
    public @ResponseBody List<Employee> getCustomers() {
        EmployeeServiceImpl c = new EmployeeServiceImpl();
        return c.getCustomers();
    }
}

rest-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<context:component-scan base-package="com.biq.controller" />
<mvc:annotation-driven />

Log

java.lang.IllegalArgumentException: No converter found for return value of type: class java.util.ArrayList
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:187)
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:174)
at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:81)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:113)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:962)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:445)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1115)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)

What exactly im missing?

shaik sha
  • 3
  • 3

3 Answers3

0

Try using @RestController instead @Controller.

Rest Controller annotation implicitly converts http response object into json/xml.

Punit
  • 324
  • 1
  • 4
  • 17
  • Register an object mapper class . Annotate this with @component and scan that package as well. Spring will automatically create the bean out of this and will use it . – Punit Sep 06 '17 at 14:44
0

Try using the latest json 2 jar. Refer this for difference between the old Json and new json 2 lib here Have below jars in your classpath:

  1. jackson-annotations-2.7.5.jar
  2. jackson-core-2.7.5.jar
  3. jackson-databind-2.7.5.jar

version can be different one. it should solve your issue. If you are using maven, then

<dependency>
   <groupId>com.fasterxml.jackson.core</groupId>
   <artifactId>jackson-databind</artifactId>
  <version>2.7.5</version>
</dependency>

This dependency will transitively add the abovelibraries to the classpath.

Sangam Belose
  • 4,262
  • 8
  • 26
  • 48
0

Register an object mapper class . Annotate this with @component and scan that package as well. Spring will automatically create the bean out of this and will use it .

@Component
public class CustomObjectMapper extends ObjectMapper {


private static final long serialVersionUID = 1L;
private static final Logger LOG = LoggerFactory.getLogger(CustomObjectMapper.class);

public CustomObjectMapper() {
    super();
    @SuppressWarnings("deprecation")
    final AnnotationIntrospector introspector = new JaxbAnnotationIntrospector();
    this.setAnnotationIntrospector(introspector);
}
}
Punit
  • 324
  • 1
  • 4
  • 17