java.lang.IllegalArgumentException: No converter found for return value of type

JavaSpringSpring BootJackson

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

The problem was that one of the nested objects in Foo didn't have any getter/setter

Solution 2 - Java

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>

Solution 3 - Java

Add the getter/setter missing inside the bean mentioned in the error message.

Solution 4 - Java

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>

Solution 5 - Java

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.

Solution 6 - Java

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.

Solution 7 - Java

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!

Solution 8 - Java

@EnableWebMvc annotation on config class resolved my problem. (Spring 5, no web.xml, initialized by AbstractAnnotationConfigDispatcherServletInitializer)

Solution 9 - Java

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.

Solution 10 - Java

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

Solution 11 - Java

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;
}

Solution 12 - Java

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.

Solution 13 - Java

you didn't have any getter/setter methods.

Solution 14 - Java

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(

Solution 15 - Java

I also experienced such error when by accident put two @JsonProperty("some_value") identical lines on different properties inside the class

Solution 16 - Java

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.

Solution 17 - Java

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.

Solution 18 - Java

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

Solution 19 - Java

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

Solution 20 - Java

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.

Solution 21 - Java

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.

Solution 22 - Java

You are missing an Annotation @ResponseBody

Solution 23 - Java

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.jacksonto 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>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMarcView Question on Stackoverflow
Solution 1 - JavaMarcView Answer on Stackoverflow
Solution 2 - JavaPAAView Answer on Stackoverflow
Solution 3 - JavaSoumyajit SwainView Answer on Stackoverflow
Solution 4 - JavaSkyWalkerView Answer on Stackoverflow
Solution 5 - JavaYubarajView Answer on Stackoverflow
Solution 6 - Javavish213View Answer on Stackoverflow
Solution 7 - JavathisisashwaniView Answer on Stackoverflow
Solution 8 - Javauser3636486View Answer on Stackoverflow
Solution 9 - JavaPowerStatView Answer on Stackoverflow
Solution 10 - Javaredd77View Answer on Stackoverflow
Solution 11 - JavaKiran KawadeView Answer on Stackoverflow
Solution 12 - JavaNilayView Answer on Stackoverflow
Solution 13 - Javaharun ugurView Answer on Stackoverflow
Solution 14 - JavashareefView Answer on Stackoverflow
Solution 15 - JavaVit IasView Answer on Stackoverflow
Solution 16 - JavaLisaView Answer on Stackoverflow
Solution 17 - JavaElectric SheepView Answer on Stackoverflow
Solution 18 - JavaparthiView Answer on Stackoverflow
Solution 19 - JavaAldo Inácio da SilvaView Answer on Stackoverflow
Solution 20 - JavaAman MalhotraView Answer on Stackoverflow
Solution 21 - JavaValdasView Answer on Stackoverflow
Solution 22 - JavaCo tiView Answer on Stackoverflow
Solution 23 - JavaPeter TarlosView Answer on Stackoverflow