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.

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
Zahid Khan
  • 1,250
  • 3
  • 15
  • 31

8 Answers8

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.

LynAs
  • 6,407
  • 14
  • 48
  • 83
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>
kryger
  • 12,906
  • 8
  • 44
  • 65
Arpit Aggarwal
  • 27,626
  • 16
  • 90
  • 108
  • 7
    made my day, just spent more the 4 hours and no one has even mentioned this any where. – Zahid Khan Oct 02 '15 at 11:26
  • 3
    Adding the dependencies was the key for me! Thanks! – Peter Oct 26 '15 at 02:43
  • 3
    The dependecies was the key for me too. I wonder why don't they write them down in Spring's page howto: http://docs.spring.io/spring-boot/docs/current/reference/html/howto-spring-mvc.html – Alfonso Nishikawa Nov 24 '15 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 '15 at 21:35
  • This magically solved my problem with the RequestBody deserialization as well. notably the error "content-type not supported" - just vanished. – Algorini Jan 09 '16 at 10:25
  • @rmpt Thanks for warning, I tried 2.7.0 just now, and it is not working, downgraded to 2.6.3 and it works fine, heh... – Betlista Jan 11 '16 at 11:47
  • Thanks gus, Any idea on why it not worked with 2.7.x. Its wasted my time for long. – sridhar Apr 20 '16 at 20:46
  • Can you confirm if the answer with checking that getters (setters) works? It seems odd that dependencies have been omitted from the documentation. – H.Rabiee Apr 21 '16 at 09:18
  • 5
    This isn't the case with spring4.x. Only adding getter/setters is enough. – thisdotnull Jun 21 '16 at 14:52
  • 1
    What getters/setters? – Hans Wouters Jul 01 '16 at 09:08
  • For me only changing the version to 2.7.9 worked. I'm using Spring 4.3.4.RELEASE. – vadipp Apr 04 '17 at 09:31
  • In this case, add getter/setter for class members of Company class or any POJO which is being returned in your case. – thisdotnull Apr 13 '17 at 19:13
  • 1
    I'm using Spring 4, and adding this dependency resolved this issue. – Sastrija Aug 11 '17 at 05:47
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) )
Jonas Czech
  • 12,018
  • 6
  • 44
  • 65
ashirman
  • 141
  • 1
  • 6
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.

pacholik
  • 8,607
  • 9
  • 43
  • 55
Arvind-MSFT
  • 131
  • 2
  • 3
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

Community
  • 1
  • 1
Rudziankoŭ
  • 10,681
  • 20
  • 92
  • 192
4

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

ovgu12
  • 130
  • 2
  • 8
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.

Suraj Kumar
  • 5,547
  • 8
  • 20
  • 42
Arvind Kumar
  • 459
  • 5
  • 21
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.

IsidroGH
  • 2,037
  • 19
  • 27