Issue
I'm doing exercises with Spring-Boot, I tried to search, but nothing I've found worked for me. I'm trying to return a JSON converting a POJO, which SB is supposed to do it automatically, but it returns:
2018-11-05 13:26:36.090 WARN 1584 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class com.example.demo.pelis.Foo]
Here is my pom.xml:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
And my POJO:
public class Foo {
private String bar;
/**
* @return the bar
*/
public String getBar() {
return bar;
}
/**
* @param bar
* the bar to set
*/
public void setBar(String bar) {
this.bar = bar;
}
}
The controller:
@RestController
public class PelisController {
@GetMapping(value = "/fooPoint")
public Foo fooPoint() {
Foo foo = new Foo();
foo.setBar("smthng");
return foo;
}
}
I don't know what I'm doing wrong, because I've tried getters/setters method, I tried to replace my pom with the one in the official guide. Nothing helped.
Solution
I just encountered this issue today and spent several hours to figure out what the issue is. Almost all of the answers I've seen are either because of missing getters and setters, adding the fasterxml dependency into the pom. I already have getters and setters and the tutorial I was following did not require those pom changes.
mariotepro's solution is what got me past the issue. His solution is buried under the comments so I decided to put it here in the answer so people can easily see it in case they have the same scenario.
So yes, just to reiterate, going into my maven repo and deleting the fasterxml folder and re-running maven is what fixed the issue for me.
Answered By - jax502
Answer Checked By - Marie Seifert (JavaFixing Admin)