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

Learn how to troubleshoot and diagnose some of the most common performance issues in Java today. Brought to you in partnership with AppDynamics.

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.

Understand the needs and benefits around implementing the right monitoring solution for a growing containerized market. Brought to you in partnership with AppDynamics.

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.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}