How to return JSON data from spring Controller using @ResponseBody
JavaJsonSpringSpring MvcSpring 4Java Problem Overview
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.
Java Solutions
Solution 1 - Java
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.
Solution 2 - Java
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>
Solution 3 - Java
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) )
Solution 4 - Java
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.
Solution 5 - Java
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
Solution 6 - Java
Yes just add the setters/getters with public modifier ;)
Solution 7 - Java
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.
Solution 8 - Java
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.