Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return JSON data from spring Controller using @ResponseBody

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.

like image 609
Zahid Khan Avatar asked Oct 02 '15 11:10

Zahid Khan


People also ask

What is the use of @ResponseBody annotation in Spring?

The @ResponseBody annotation tells a controller that the object returned is automatically serialized into JSON and passed back into the HttpResponse object. When you use the @ResponseBody annotation on a method, Spring converts the return value and writes it to the HTTP response automatically.

How do I return a JSON response?

To return JSON from the server, you must include the JSON data in the body of the HTTP response message and provide a "Content-Type: application/json" response header. The Content-Type response header allows the client to interpret the data in the response body correctly.

What is @RequestBody and @ResponseBody annotation in Spring?

@RequestBody and @ResponseBody annotations are used to bind the HTTP request/response body with a domain object in method parameter or return type. Behind the scenes, these annotation uses HTTP Message converters to convert the body of HTTP request/response to domain objects.


2 Answers

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.

like image 91
LynAs Avatar answered Oct 10 '22 02:10

LynAs


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> 
like image 37
Arpit Aggarwal Avatar answered Oct 10 '22 02:10

Arpit Aggarwal