61

Spring version 4.2.0, Hibernate 4.1.4 Here is my Controller function:

@RequestMapping(value = "/mobile/getcomp", method = RequestMethod.GET)
@ResponseBody
public List<Company>  listforCompanies() {      
    List<Company> listOfCompanies= new ArrayList<Company>();        
    listOfCompanies = companyManager.getAllCompanies();
    return listOfCompanies;
}

Jackson JSON mapper dependency in Pom.xml:

    <!-- Jackson JSON Mapper -->
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>${jackson.version}</version>
    </dependency>

Getting the list in my ArrayList, but when returning the following error is shown:

SEVERE: Servlet.service() for servlet [dispatcherServlet] in context with path [/IrApp] 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
    java.lang.IllegalArgumentException: No converter found for return value of type: class java.util.ArrayList
        at org.springframework.util.Assert.isTrue(Assert.java:68)
        at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:124)

Link to the example I'm following.

4
  • can you post your spring configuration plz ? Oct 2, 2015 at 11:13
  • @RafikBELDI mate every thing is fine is spring config not able to add too much code in question as it demands description.
    – Zahid Khan
    Oct 2, 2015 at 11:27
  • 11
    My problem was that all getters was private.
    – Grim
    Feb 28, 2016 at 7:34
  • Thanks, @peter . My getters were package-private, but they needed to be public.
    – Dalbergia
    Jan 10, 2017 at 16:34

8 Answers 8

110

I was facing same issue. I did not put @ResponseBody since I was using @RestController. But still I was getting error because I did not put the getter/setter method for the Company class. So after putting the getter/setter my problem was resolved.

7
  • 22
    Adding just the getter was sufficient. Mar 22, 2016 at 20:49
  • 6
    This is the correct answer. One should not add more dependencies.
    – H.Rabiee
    Apr 21, 2016 at 9:17
  • Thanks the same worked for me. But little surprised how that really matters. Jul 13, 2016 at 18:03
  • 1
    Worked for me. In my case setting field access to public worked as well.
    – dorr
    Jul 25, 2016 at 8:20
  • yes this works. Spring should fix that error to be more specific.
    – Fraggle
    Mar 11, 2017 at 16:30
106

Add the below dependency to your pom.xml:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.5.0</version>
</dependency>
13
  • 7
    made my day, just spent more the 4 hours and no one has even mentioned this any where.
    – Zahid Khan
    Oct 2, 2015 at 11:26
  • 3
    Adding the dependencies was the key for me! Thanks!
    – Peter
    Oct 26, 2015 at 2:43
  • 3
    The dependecies was the key for me too. I wonder why don't they write them down in Spring's page howto: docs.spring.io/spring-boot/docs/current/reference/html/… Nov 24, 2015 at 20:28
  • 3
    worked for me too! A little note for users passing by and always choose to use the last version of the dependency instead of the mentioned here. DO NOT USE THE LAST VERSION for jackson dependecy! I've tried with 2.7.0-rc1 and didn't work. With 2.6.3 worked just fine.
    – rmpt
    Nov 30, 2015 at 21:35
  • 5
    This isn't the case with spring4.x. Only adding getter/setters is enough. Jun 21, 2016 at 14:52
12

You also need to be sure that returned bean is not empty (and can be serialized by Jackson). In my particular case I tried to return an instance of an object without getters and setters and without any jackson annotation and with fields equals to null. I got following message:

com.fasterxml.jackson.databind.JsonMappingException:
    No serializer found for class com.foo.bar.Baz and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) )
1
  • 2
    This is more a comment (not an answer).
    – jogo
    Jan 14, 2016 at 14:51
8

When I was facing this issue, I simply put just getter setter methods and my issues were resolved.

I am using Spring boot version 2.0.

1
  • adding getter & setter will now fix the issue in this case
    – CodeIsLife
    Mar 2, 2017 at 21:30
5

Considering @Arpit answer, for me it worked only when I add two jackson dependencies:

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

and configured, of cause, web.xml <mvc:annotation-driven/>.

Original answer that helped me is here: https://stackoverflow.com/a/33896080/3014866

4

Yes just add the setters/getters with public modifier ;)

3

I was using groovy+springboot and got this error.

Adding getter/setter is enough if we are using below dependency.

implementation 'org.springframework.boot:spring-boot-starter-web'

As Jackson core classes come with it.

0

In my case I was using jackson-databind-2.8.8.jar that is not compatible with JDK 1.6 I need to use so Spring wasn't loading this converter. I downgraded the version and it works now.

Not the answer you're looking for? Browse other questions tagged or ask your own question.