Spring RestController - No converter found for return value of type java.lang.Integer

I am currently trying to create my first rest service with spring, I wan't him to return XML result (based on JAXB).

on a very basic rest controller:

import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;

@RestController
public class CVIController {
    @RequestMapping(value = "/resume")
    public @ResponseBody
    Integer getResume() {
        return 5;
    }
}

with a spring-servlet.xml pretty empty:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <context:component-scan base-package="fr.urouen"
    />
    <mvc:annotation-driven />
</beans>

And a Tomcat server under JDK8 so I can be sure that all modules are load by default on the JVM... I got the following exception throwed:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: No converter found for return value of type: class java.lang.Integer

I don't wan't to use Jackson because I know that JAXB can handle this. But after juggle with some part of my code, I still can't found the answer of why she is throwed.

Edit 1:

I tried to create a dummy class with getters as answers said

public class Dummy {
    private int value;


    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }
}

@RestController
public class CVIController {
    @RequestMapping(value = "/resume")
    public ResponseEntity<Dummy> getResume() {
        Dummy dummy = new Dummy();
        dummy.setValue(5);
        return new ResponseEntity<Dummy>(dummy, HttpStatus.OK);
    }
}

But still got the exact same result :

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: No converter found for return value of type: class fr.urouen.model.Dummy

The simplest solution is to return a String and remove the @ResponseBody annotation. As a rest controller, by definition, returns a string, that's the way to go for this specific example.

If you are after a more comprehensive solution, Spring by default uses Jackson to do the conversion from object to JSON. The @ResponseBody does that automatically.

Im pretty sure that Integer won't work because it's not a POJO with getters and setters and something that can be converted to JSON. ie { 5 } is not a valid JSON object.

if you had a class like this:

public class returnObject() {
  private int value;
  ... getters/setters
}

and then did this:

@RestController
@ResponseBody
public ReturnObject getValue() {
   ReturnObject ret = new ReturnObject();
   ret.setValue(5);
   return ret;
}

You would then get this:

{ "value" : 5 }

You will also need to add jackson to your pom:

<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.9.4</version>
</dependency>

java.lang.IllegalArgumentException: No converter found for return , IllegalArgumentException: No converter found for return value of type · java spring spring-boot jackson. With this code @RequestMapping(value  Spring Boot RestController Error: “No Converter Found for Return Value of Type” Let's take a look at how this happens and how public getters are one possible solution to the problem. by

ResponseEntity is used for a return some content from restcontroller, Have you tried with ResponseEntity ?

@RestController
public class CVIController {
    @RequestMapping(value = "/resume")
    public @ResponseBody ResponseEntity<?> getResume() {
        return ResponseEntity.ok(5);
    }
}

or you can write

@RestController
public class CVIController {
    @RequestMapping(value = "/resume")
    public @ResponseBody ResponseEntity<Integer> getResume() {
        return new ResponseEntity<Integer>(5, HttpStatus.OK);
    }
}

more example you can get from here

Spring Boot RestController Error: “No converter found for return , 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. Spring Boot RestController Error: “No converter found for return value of type” 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 met this promblem too,try download jackson-databind.jar,ackson-core.jar, ackson-annotations.jar and put them in your WEB-INF/lib directory

No converter found for return value of type: class java.util , No converter found for return value of type: class java.util.LinkedHashMap # doGet(FrameworkServlet.java:861) ~[spring-webmvc-4.3.15. Exception when Using Spring MVC 4 : No converter found for return value of type: class java.util.ArrayList 30 Aug , 2016 2 Comments Share I had this exception when moving from Spring MVC 3 to SPring MVC 4, and trying to use @RestController.

RestController is not able to convert response body in json format , RestController is not able to convert response body in json format #160. Closed No converter found for return value of type: class java.util. So the problem looks like spring is unable to convert a complex type to json. RestController is not able to convert response body in RestController is not able to convert response No converter found for return value of type: class java

No converter found for return value of type: class java.util.ArrayList, I had this exception when moving from Spring MVC 3 to SPring MVC 4, and trying to use @RestController. Here is the complete stacktrace,  Home » Java » No converter found for return value of type: class java.util.ArrayList (Spring Boot) No converter found for return value of type: class java.util.ArrayList (Spring Boot) Posted by: admin July 15, 2018 Leave a comment

Spring Boot: Solution for "No converter found for return value of type , By default, the java object returned by @RestController (the following User) is no problem. // controller @RestController public class ZezeController { @  Spring Boot RestController Error: “No Converter Found for Return Value of Type” Spring Boot Error: Creating a Bean With 'DataSource' Defined in DataSourceAutoConfiguration Free DZone Refcard

Comments
  • Unfortunately, I've try with classes simple as ReturnObject but it still doesn't works. If I'm returning a String, I am suppose to build her (with JAXBContext ect ?... Pretty uncomfortable for a Framework like that).
  • try adding <mvc:default-servlet-handler /> and <context:annotation-config /> to your xml.
  • Have you added any marshaling lib like Jackson or Gson ?
  • No because as I said, I don't wan't to use Jackson because I know that JAXB can handle this (like many online tutorial said)
  • For now I have a working solution with Jackson, but I would like to have the one with JAXB.
  • oky I read one blog admfactory.com/…, and there is not mentioned Integer class in data type, so that's the reason JAXB not understanding, try with BigInteger or just int as return type.
  • Or you can directly return java.lang.Object