127

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?

İsmail Y.
  • 3,579
  • 5
  • 21
  • 29
Marc
  • 16,170
  • 20
  • 76
  • 119
  • 1
    solution https://stackoverflow.com/questions/41719142/how-to-return-a-set-of-objects-with-spring-boot/46977580#46977580 – Akshay Vijay Jain Oct 27 '17 at 14:40
  • Use Central repository to download jars If you are not maven user here you can find your required version jars http://repo1.maven.org/maven2/com/fasterxml/jackson/core/ – Vivek Mishra Apr 04 '18 at 02:17

23 Answers23

353

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

Naman
  • 27,789
  • 26
  • 218
  • 353
Marc
  • 16,170
  • 20
  • 76
  • 119
  • 19
    it needs only getters – Oleksandr Loushkin May 20 '17 at 13:03
  • 5
    This resolved my issue also. If anyone continues receiving a Converter error, it the most likely cause is that Jackson cannot find/process the necessary getter(s). Check your POJO getter's closely! In my case, I had a lower-cased getter on one property instead of the expected camel case: getmetaTag() instead of getMetaTag() – Michael M Jun 07 '17 at 17:31
  • 1
    Also make sure that the getter/setters are `public`! (Or if you, like me, are using immutable classes, make sure your fields are `public final` not just `final`) – Nic Nov 16 '18 at 04:53
  • 2
    Thanks your answer helped, but in my case I had two identical json property names assigned to two separated fields. i.e. @JsonProperty("id")... @JsonProperty("id"). Looks like this error gets thrown whenever the jackson serializer fails. – Aaron Sep 06 '19 at 17:43
  • You can also makes class attributes public if you do not want to add getters. – Trismegistos Dec 18 '20 at 13:44
  • This answer might be specific to Spring Framework. I'm almost positive I didn't run into this issue when using JAX-RS and JBoss (I assume it used reflection to get the values instead of accessors). – TastyWheat Nov 14 '22 at 15:32
23

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>
PAA
  • 1
  • 46
  • 174
  • 282
17

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

Soumyajit Swain
  • 1,298
  • 1
  • 21
  • 35
13

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>
SkyWalker
  • 28,384
  • 14
  • 74
  • 132
  • it's spring-boot. I don't have a mvc-dispatcher-servlet. Do you know if there's a property for that? – Marc Jun 15 '16 at 17:04
  • 1
    Use getter/setter for foo class – SkyWalker Jun 15 '16 at 17:08
  • I don't understand what you mean by that. The problem is the object that I send not that I receive. There's no getter/setter – Marc Jun 15 '16 at 17:09
  • @Marc Please go through this tutorial: http://sanjayingole.blogspot.com/2015/10/no-converter-found-for-return-value-of.html – SkyWalker Jun 15 '16 at 17:11
  • Yeah but this is spring. I'm using Spring boot. The configuration is different – Marc Jun 15 '16 at 17:13
  • For Spring boot, http://stackoverflow.com/questions/33832735/spring-boot-application-no-converter-found-for-return-value-of-type – SkyWalker Jun 15 '16 at 17:17
  • Where do I set the message-converters. I don't have a mvc-dispatcher-servlet.xml – Marc Jun 15 '16 at 17:20
  • 1
    @Marc I know this wasn't your problem, but you can add message converters by adding a `org.springframework.boot.autoconfigure.web.HttpMessageConverters` bean with a `@Bean` annotated method in your `@SpringBootApplication` class. You can see this by just reading the `org.springframework.boot.autoconfigure.web.WebMvcAutoconfiguration` class, the Spring boot autoconfiguration code is incredibly readable and often the quickest way to figure out problems like these – UTF_or_Death Jul 04 '17 at 14:16
  • 2
    Adding "MappingJackson2HttpMessageConverter" worked for me. Thanks man. I am using Spring 4.2.5 and was facing this problem. I added above converter in WebMvcConfigurerAdapter. – Hemant Nagpal Dec 15 '17 at 13:18
8

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.

Yubaraj
  • 3,800
  • 7
  • 39
  • 57
5

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.

vish213
  • 748
  • 3
  • 12
  • 27
5

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

user3636486
  • 409
  • 1
  • 5
  • 11
3

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!

thisisashwani
  • 1,748
  • 2
  • 18
  • 25
3

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.

Nilay
  • 79
  • 9
2

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.

PowerStat
  • 3,757
  • 8
  • 32
  • 57
2

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

redd77
  • 307
  • 5
  • 11
  • This solved the problem changed this `@RequestMapping(value = "/", produces = MediaType.ALL_VALUE)` to this `@RequestMapping(value = "/", produces = MediaType.APPLICATION_JSON_VALUE)` – eli Feb 04 '21 at 22:08
2

you didn't have any getter/setter methods.

harun ugur
  • 1,718
  • 18
  • 18
2

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(
shareef
  • 9,255
  • 13
  • 58
  • 89
1

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;
}
  • If you have a new question, please ask it by clicking the [Ask Question](/questions/ask) button. Include a link to this question if it helps provide context. – iBug Jan 05 '18 at 04:43
0

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

Vit Ias
  • 725
  • 4
  • 16
0

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.

Lisa
  • 11
  • 2
0

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.

Electric Sheep
  • 3,867
  • 1
  • 29
  • 38
0

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

parthi
  • 101
  • 8
0

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
Aldo Inácio da Silva
  • 824
  • 2
  • 14
  • 38
0

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.

0

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.

Valdas
  • 1,074
  • 13
  • 20
0

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>
Peter Tarlos
  • 731
  • 10
  • 13
-1

You are missing an Annotation @ResponseBody

Co ti
  • 124
  • 2
  • 13