0

I'm trying to send list of customers as JSON response, but, its returning with error response as

 No converter found for return value of type: class java.util.ArrayList

Config class

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "net.ifg.spring")
public class AppConfig {

@Bean
public Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder() {
    Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
    return builder;
}
}

My controller is

@RestController
public class CustomerRestController {

    @Autowired
    private CustomerDAO customerDAO;

    @GetMapping("/customers")
    public List<Customer> getCustomers() {
        return customerDAO.list();
    }

}

Customer class

public class Customer {

private Long id;
private String firstName;


public Customer(long id, String firstName) {
    this.id = id;
    this.firstName = firstName;     
}

public Customer() {
}

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}


}

JARs added

commons-logging-1.2.jar, jackson-core-asl-1.9.13.jar, jackson-mapper-asl-1.9.13.jar, jackson-databind-2.9.0.jar, spring-aop,spring-core,spring-beans,spring-context, spring-expression, spring-web, spring-webmvc(all 4.3.9 version)

Still its returning me error response.

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: No converter found for return value of type: class java.util.ArrayList
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

Any ideas would be greatly appreciated

Aliy
  • 197
  • 14
  • Just a guess here but try using an array instead (or wrap the list inside some object). – Thomas Sep 06 '17 at 10:40
  • Show the dependencies you've added and the `Customer` class as well. – cassiomolin Sep 06 '17 at 10:40
  • Possible duplicate of [How to return a list of object as Json in Spring MVC](https://stackoverflow.com/questions/12746382/how-to-return-a-list-of-object-as-json-in-spring-mvc) – David Lavender Sep 06 '17 at 10:42
  • is it the same problem ? https://stackoverflow.com/questions/32905917/how-to-return-json-data-from-spring-controller-using-responsebody – razor Sep 06 '17 at 10:48
  • @CássioMazzochiMolin, updated my question along with jar details and customer class – Aliy Sep 06 '17 at 10:56
  • @razor, the requirement is same, but I have added all possible ways to get the result – Aliy Sep 06 '17 at 10:59
  • @Aliy add the implements Serializable to your Customer class – cralfaro Sep 06 '17 at 11:04
  • @cralfaro.. No still the issue exists – Aliy Sep 06 '17 at 11:08
  • Are you using Spring Boot? – cassiomolin Sep 06 '17 at 11:08
  • @Aliy just to be sure then, replace RestController by Controller and add ResponseBody to your get method, just to check if works in the basic scenario, should be the same as you have right now, but to check it – cralfaro Sep 06 '17 at 11:11
  • @CássioMazzochiMolin, I'm using annotation based spring where I've only config – Aliy Sep 06 '17 at 11:11
  • @CássioMazzochiMolin, but I don't have any other spring boot jars. – Aliy Sep 06 '17 at 11:13
  • @cralfaro, its still same, I have replaced RestController with Controller and updated my method as public ResponseBody List getCustomers() – Aliy Sep 06 '17 at 11:14

2 Answers2

-1

Add Jackson dependency or jar in your project.

Like,

<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>VERSION</version></dependency>
vikas
  • 270
  • 2
  • 11
  • Please note that as of Spring Framework 4.3, the minimum requirement is Jackson 2.6. https://github.com/spring-projects/spring-framework/wiki/Migrating-to-Spring-Framework-4.x#third-party-dependencies – Roy Clarkson Sep 06 '17 at 12:07
  • @RoyClarkson, My jackson-databind version is 2.9 – Aliy Sep 06 '17 at 12:14
  • Could you please go throw this https://www.leveluplunch.com/java/tutorials/023-configure-integrate-gson-spring-boot/ .It might help full for you.Configure your custom customConverters(HttpMessageConverters.class) – vikas Sep 06 '17 at 13:50
-1

If this is being made in Spring MVC then the dispatcher-servlet's xml file must have <mvc:annotation-driven />tag for conversion to work.

Akhil
  • 1