Reputation: 16512
With this code
@RequestMapping(value = "/bar/foo", method = RequestMethod.GET)
public ResponseEntity<foo> foo() {
Foo model;
...
return ResponseEntity.ok(model);
}
}
I get the following exception
java.lang.IllegalArgumentException: No converter found for return value of type
My guess is that the object cannot be converted to JSON because Jackson is missing. I don't understand why because I thought that Jackson was built in with spring boot.
Then I have tried to add Jackson to the pom.xml but I still have the same error
<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>
Do I have to change any spring boot properties to make this work?
Upvotes: 131
Views: 193155
Reputation: 153
In Response Object Or its nested object(s), their attributes
Upvotes: 0
Reputation: 901
I also encountered the same error on a Spring 5 project (not Spring Boot), by running a SpringMVC JUnit test-case on a method that returns ResponseEntity<List<MyPojo>>
Error: No converter found for return value of type: class java.util.ArrayList
I thought I had all the correct Jackson artifacts in my pom, but later realized that I had the legacy versions. The Maven groupId changed on the Jackson jars from org.codehaus.jackson
to com.fasterxml.jackson.core
. After switching to the new jars the error went away.
Updated maven pom.xml:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.7</version>
</dependency>
Upvotes: 0
Reputation: 1074
This might also happen due low Jackson version; e.g. Spring Boot 2.4 default Jackson version is too low when using Java records; you need at least 2.5 to serialize them properly.
Upvotes: 0
Reputation: 97
Add below dependency in pom.xml:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.10.1</version>
</dependency>
Was facing the same issue as the return type cannot be bind with the MediaType of Class Foo. After adding the dependency it worked.
Upvotes: 0
Reputation: 3988
The answer written by @Marc is also valid. But the concrete answer is the Getter
method is required. You don't even need a Setter
.
Upvotes: 8
Reputation: 9581
In my case, I was returning Boolean in Response Entity and had :
produces = MediaType.TEXT_PLAIN_VALUE,
When i changed it to below
produces = MediaType.APPLICATION_JSON_VALUE
It worked!
Example of what i had.
@PostMapping(value = "/xxx-xxxx",
produces = MediaType.APPLICATION_JSON_VALUE,
consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Boolean> yyyy(
Upvotes: 2
Reputation: 16512
The problem was that one of the nested objects in Foo
didn't have any getter/setter
Upvotes: 357
Reputation: 337
In my case i'm using spring boot , and i have encountered a similar error :
No converter for [class java.util.ArrayList] with preset Content-Type 'null'
turns out that i have a controller with
@GetMapping(produces = { "application/xml", "application/json" })
and shamefully i wasn't adding the Accept
header to my requests
Upvotes: 2
Reputation: 988
I faced the same problem but I was using Lombok and my UploadFileResponse pojo was a builder.
public ResponseEntity<UploadFileResponse>
To solve I added @Getter annotation:
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Getter
public class UploadFileResponse
Upvotes: 0
Reputation: 3821
While using Spring Boot 2.2 I run into a similiar error message and while googling my error message
No converter for [class java.util.ArrayList] with preset Content-Type 'null'
this question here is on top, but all answers here did not work for me, so I think it's a good idea to add the answer I found myself:
I had to add the following dependencies to the pom.xml
:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
</dependency>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.11.1</version>
</dependency>
After this I need to add the following to the WebApplication class:
@SpringBootApplication
public class WebApplication
{
// ...
@Bean
public HttpMessageConverter<Object> createXmlHttpMessageConverter()
{
final MarshallingHttpMessageConverter xmlConverter = new MarshallingHttpMessageConverter();
final XStreamMarshaller xstreamMarshaller = new XStreamMarshaller();
xstreamMarshaller.setAutodetectAnnotations(true);
xmlConverter.setMarshaller(xstreamMarshaller);
xmlConverter.setUnmarshaller(xstreamMarshaller);
return xmlConverter;
}
}
Last but not least within my @Controller
I used:
@GetMapping(produces = {MediaType.APPLICATION_XML_VALUE, MediaType. APPLICATION_JSON_VALUE})
@ResponseBody
public List<MeterTypeEntity> listXmlJson(final Model model)
{
return this.service.list();
}
So now I got JSON and XML return values depending on the requests Accept
header.
To make the XML output more readable (remove the complete package name from tag names) you could also add @XStreamAlias
the following to your entity class:
@Table("ExampleTypes")
@XStreamAlias("ExampleType")
public class ExampleTypeEntity
{
// ...
}
Hopefully this will help others with the same problem.
Upvotes: 2
Reputation: 101
Faced same error recently - the pojo had getters/setters and all jackson dependencies were imported in pom correctly but some how "< scope > " was "provided" for jackson dependency and this caused the issue. Removing " < Scope > " from jackson dependency fixed the issue
Upvotes: 0
Reputation: 11975
Add the below dependency to your pom.xml:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.0.pr3</version>
</dependency>
Upvotes: 23
Reputation: 4320
I saw the same error when the scope of the jackson-databind
dependency had been set to test
:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.9</version>
<scope>test</scope>
</dependency>
Removing the <scope>
line fixed the issue.
Upvotes: 0
Reputation: 429
@EnableWebMvc annotation on config class resolved my problem. (Spring 5, no web.xml, initialized by AbstractAnnotationConfigDispatcherServletInitializer)
Upvotes: 5
Reputation: 129
I was facing same issue for long time then comes to know have to convert object into JSON using Object Mapper and pass it as JSON Object
@RequestMapping(value = "/getTags", method = RequestMethod.GET)
public @ResponseBody String getTags(@RequestParam String tagName) throws
JsonGenerationException, JsonMappingException, IOException {
List<Tag> result = new ArrayList<Tag>();
for (Tag tag : data) {
if (tag.getTagName().contains(tagName)) {
result.add(tag);
}
}
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(result);
return json;
}
Upvotes: 1
Reputation: 79
I was getting the same error for a while.I had verify getter methods were available for all properties.Still was getting the same error. To resolve an issue Configure MVC xml(configuration) with
<mvc:annotation-driven/>
.This is required for Spring to detect the presence of jackson and setup the corresponding converters.
Upvotes: 3
Reputation: 1340
Add the getter/setter missing inside the bean mentioned in the error message.
Upvotes: 17
Reputation: 11
In my case, I forgot to add library jackson-core.jar, I only added jackson-annotations.jar and jackson-databind.jar. When I added jackson-core.jar, it fixed the problem.
Upvotes: 0
Reputation: 786
The issue occurred in my case because spring framework couldn't fetch the properties of nested objects. Getters/Setters is one way of solving. Making the properties public is another quick and dirty solution to validate if this is indeed the problem.
Upvotes: 5
Reputation: 1844
I had the very same problem, and unfortunately it could not be solved by adding getter methods, or adding jackson dependencies.
I then looked at Official Spring Guide, and followed their example as given here - https://spring.io/guides/gs/actuator-service/ - where the example also shows the conversion of returned object to JSON format.
I then again made my own project, with the difference that this time I also added the dependencies and build plugins that's present in the pom.xml file of the Official Spring Guide example I mentioned above.
The modified dependencies and build part of XML file looks like this!
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
You can see the same in the mentioned link above.
And magically, atleast for me, it works. So, if you have already exhausted your other options, you might want to try this out, as was the case with me.
Just a side note, it didn't work for me when I added the dependencies in my previous project and did Maven install and update project stuff. So, I had to again make my project from scratch. I didn't bother much about it as mine is an example project, but you might want to look for that too!
Upvotes: 3
Reputation: 803
I also experienced such error when by accident put two @JsonProperty("some_value") identical lines on different properties inside the class
Upvotes: 0
Reputation: 29130
Use @ResponseBody
and getter/setter
. Hope it will solve your issue.
@RequestMapping(value = "/bar/foo", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<foo> foo() {
and update your mvc-dispatcher-servlet.xml
:
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>
Upvotes: 14