Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Spring Boot RestController Error: “No Converter Found for Return Value of Type”

DZone's Guide to

Spring Boot RestController Error: “No Converter Found for Return Value of Type”

Have you come across this error? Let's take a look at how this happens and how public getters are one possible solution to the problem.

· Java Zone
Free Resource

What every Java engineer should know about microservices: Reactive Microservices Architecture.  Brought to you in partnership with Lightbend.

Spring Boot RestControllers, by default, can return a POJO class as the return result from a mapped request method, and it is converted into a JSON result. I’ve run into this issue before though, and it’s not immediately obvious what’s wrong: ‘No converter found for return value of type’:

java.lang.IllegalArgumentException: No converter found for return value 
of type: class kh.springboot.redis.domain.RedisResult
at org.springframework.web.servlet.mvc.method.annotation
.AbstractMessageConverterMethodProcessor
.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:187) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE]


My returned class from my @GetMapping method is just a simple POJO:

public class RedisResult {

    private String key;
    private String value;

    public RedisResult(String key, String value) {
        this.key = key;
        this.value = value;
    }
}


And my @RestController is a simple controller with a single @GetMapping (in this case I’m building a REST endpoint to query key values from Redis using Spring Data Redis RedisTemplate:

@RestController

public class RedisRestController {

    @Autowired
    @Qualifier("RedisTemplate")
    private RedisTemplate template;

    @GetMapping("singlekey/{key}")
    public RedisResult getSingleValue(@PathVariable("key") String key){
        String value = (String)this.template.opsForValue().get(key);
        RedisResult result = new RedisResult(key, value);
        return result;
    }
}


If you’ve found my post because you have this same issue, before you go down the rabbit hole adding additional Maven dependencies or additional annotations you think might be missing, the reason is usually that the POJO class you are returning doesn’t have any public getter methods. For each property in your POJO that you want returned in your JSON, make sure you have a public getter.

This is discussed in this StackOverflow post here.

Microservices for Java, explained. Revitalize your legacy systems (and your career) with Reactive Microservices Architecture, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:
java ,tutorial ,spring boot errors

Published at DZone with permission of Kevin Hooke, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}