18

I am writing a simple REST API according to this Spring-Boot tutorial. On my local dev machines (Ubuntu 15.04 and Windows 8.1) everything works like a charm.

I have an old 32-bit Ubuntu 12.04 LTS server lying around on which I wanted to deploy my REST service.

The starting log is ok, but as soon as I send a GET request to the /user/{id} endpoint, I get the following error:

java.lang.IllegalArgumentException: No converter found for return value of type: class ch.gmazlami.gifty.models.user.User

And then down the stacktrace:

java.lang.IllegalArgumentException: No converter found for return value of type: class java.util.LinkedHashMap

The entire stacktrace is posted here.

I looked into some answers referring this error, but those don't seem to apply to my problem, since I'm using Spring-Boot, no xml configs whatsoever.

The affected controller is:

    @RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
public ResponseEntity<User> getUser(@PathVariable Long id){
    try{
        return new ResponseEntity<User>(userService.getUserById(id), HttpStatus.OK);
    }catch(NoSuchUserException e){
        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }
}

Any help would be greatly appreciated. It is very weird since the exact same things work on other machines perfectly.

icy
  • 1,468
  • 3
  • 16
  • 36
gmazlami
  • 684
  • 2
  • 9
  • 18

6 Answers6

33

This happened to me, on one resource only (one method) and I did not understand why. All methods within classes in the same package, with the same annotations, same call to ResponseEntity.ok(...) etc. just worked.

But not this one.

It turns out I had forgottent to generate the getters on my POJO class !

As soon as I had added them it worked.

Hopefully it can save somebody some time eventually...

Pierre Henry
  • 16,658
  • 22
  • 85
  • 105
  • 5
    This fixed my problem too. If anyone tracking this post is still having issues, check your POJO getter's closely; in my case, I had a lower-cased getter on one property; instead of the expected camel case: gettargetObject() instead of getTargetObject() – Michael M Jun 07 '17 at 17:27
  • 2
    Same with me..I had forgotten to add getters in my POJO class. Plus1 – Tushar Thakur Jan 23 '19 at 11:34
  • 2
    Works like a charm. with SpringBoot 2.0+, no need to manually add dependency for jackson, no need to register MessageConverter manually, what u need is just getters for ur POJO. – reniaL Mar 13 '19 at 07:45
19

you should make some changes to your pom.xml and mvc-dispatcher-servlet.xml files: Add the following dependecies to your pom.xml :

<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>

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>
Yahia Ammar
  • 280
  • 4
  • 20
9

This happens when you forget the "build" call:

return ResponseEntity.status(HttpStatus.BAD_REQUEST);

should be:

return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
Kaan Yy
  • 471
  • 6
  • 9
5

I meet with this problem, because I omitted Getters and Setters method.

duyuanchao
  • 3,863
  • 1
  • 25
  • 16
1

To add to the rest of the answers: the methods must be public.

My IDE flagged that the methods could be "package only", prompting me to remove the "public" portion of the declaration (which I foolishly did).

I added public to my methods and solved the problem.

JACH
  • 996
  • 11
  • 20
0

I was using IntelliJ Idea and its auto-generated getters and setters. Since I had a boolean field called success, the getter was named isSucccess(). I renamed it getSuccess() and the error went away.