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.
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.
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.
@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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With