How to return JSON data from spring Controller using @ResponseBody
Asked Answered
F

8

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.

Feeling answered 2/10, 2015 at 11:11 Comment(4)
can you post your spring configuration plz ?Silurid
@RafikBELDI mate every thing is fine is spring config not able to add too much code in question as it demands description.Feeling
My problem was that all getters was private.Requite
Thanks, @peter . My getters were package-private, but they needed to be public.Tallent
O
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>
Overwinter answered 2/10, 2015 at 11:20 Comment(13)
made my day, just spent more the 4 hours and no one has even mentioned this any where.Feeling
Adding the dependencies was the key for me! Thanks!Zaremski
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/…Athene
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.Mateusz
This magically solved my problem with the RequestBody deserialization as well. notably the error "content-type not supported" - just vanished.Brobdingnagian
@Mateusz 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...Progestin
Thanks gus, Any idea on why it not worked with 2.7.x. Its wasted my time for long.Tacitus
Can you confirm if the answer with checking that getters (setters) works? It seems odd that dependencies have been omitted from the documentation.Gimpel
This isn't the case with spring4.x. Only adding getter/setters is enough.Calen
What getters/setters?Smew
For me only changing the version to 2.7.9 worked. I'm using Spring 4.3.4.RELEASE.Enfold
In this case, add getter/setter for class members of Company class or any POJO which is being returned in your case.Calen
I'm using Spring 4, and adding this dependency resolved this issue.Savaii
P
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.

Putrid answered 6/3, 2016 at 2:42 Comment(7)
Adding just the getter was sufficient.Ferrante
This is the correct answer. One should not add more dependencies.Gimpel
Thanks the same worked for me. But little surprised how that really matters.Vingtetun
Worked for me. In my case setting field access to public worked as well.Elegist
yes this works. Spring should fix that error to be more specific.Hymenopterous
If you are using lombok, don't forget to put @Data for gettersObligate
I tried to add using Lombok getter and setter but it did not worked out until i added all conventional getters/setters. Any one have any idea on this??Castera
O
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>
Overwinter answered 2/10, 2015 at 11:20 Comment(13)
made my day, just spent more the 4 hours and no one has even mentioned this any where.Feeling
Adding the dependencies was the key for me! Thanks!Zaremski
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/…Athene
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.Mateusz
This magically solved my problem with the RequestBody deserialization as well. notably the error "content-type not supported" - just vanished.Brobdingnagian
@Mateusz 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...Progestin
Thanks gus, Any idea on why it not worked with 2.7.x. Its wasted my time for long.Tacitus
Can you confirm if the answer with checking that getters (setters) works? It seems odd that dependencies have been omitted from the documentation.Gimpel
This isn't the case with spring4.x. Only adding getter/setters is enough.Calen
What getters/setters?Smew
For me only changing the version to 2.7.9 worked. I'm using Spring 4.3.4.RELEASE.Enfold
In this case, add getter/setter for class members of Company class or any POJO which is being returned in your case.Calen
I'm using Spring 4, and adding this dependency resolved this issue.Savaii
P
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) )
Protoxide answered 14/1, 2016 at 14:33 Comment(1)
This is more a comment (not an answer).Salisbarry
I
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.

Ima answered 25/2, 2017 at 12:0 Comment(1)
adding getter & setter will now fix the issue in this caseXerophthalmia
G
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://mcmap.net/q/161946/-spring-boot-application-no-converter-found-for-return-value-of-type

Gantt answered 6/8, 2016 at 11:24 Comment(0)
L
4

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

Lagrange answered 30/4, 2020 at 18:8 Comment(0)
M
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.

Mandie answered 29/6, 2019 at 12:40 Comment(0)
T
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.

Titanite answered 6/11, 2017 at 10:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.