0

I am getting the following exception and not sure why...

Exception in thread "main" org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.avada.rest.UsersController$Users] and content type [application/json;charset=UTF-8] at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:109) at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:576) at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:529) at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:236) at com.avada.rest.UsersTest.main(UsersTest.java:18)

This is my RestController:

@RestController
@RequestMapping("/users")
public class UsersController {

    @RequestMapping(method = RequestMethod.GET)
    public Users getUsers() {
        Users users = new Users();
        users.setUsers(ConstantsHome.userprofileMgr.getUsers(null));
        return users;
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public User getUser(@PathVariable String id) {
        return ConstantsHome.userprofileMgr.getUserByUserId(id, true, true);
    }

    public static class Users {
        private List<User> users = new ArrayList<>();

        public List<User> getUsers() {
            return users;
        }

        public void setUsers(List<User> users) {
            this.users = users;
        }
    }
}

This is my Test class:

public class UsersTest {
    private static RestTemplate template = new RestTemplate();

    public static void main (String[] args) throws Exception {
        // Get all users
        String uri = "http://localhost:8080/IR360/rest/users";
        UsersController.Users users = template.getForObject(uri, UsersController.Users.class);
        System.out.println("Looping through users...");
        for (User user : users.getUsers()) {
            System.out.println("Name=" + user.getName());
        }

        // Get 1 user
        uri = "http://localhost:8080/IR360/rest/users/admin";
        User user = template.getForObject(uri, User.class);
        System.out.println("Name for single user=" + user.getName());
    }
}

I can get a single user no problem if I comment out the test code for "Get all users".

What am I doing wrong in this code?

P.S. - I can make a call to getUsers() through the browser and the json comes back fine so I know getUsers() is working...just can't get the RestTemplate to work

Zack Macomber
  • 6,682
  • 14
  • 57
  • 104
  • Add an instance of `MappingJackson2HttpMessageConverter` to your `RestTemplate`'s list of converters during construction – Ali Dehghani Feb 05 '16 at 17:55
  • All of the following converters are present in my `RestTemplate`: 0 = {ByteArrayHttpMessageConverter@1366} 1 = {StringHttpMessageConverter@1367} 2 = {ResourceHttpMessageConverter@1368} 3 = {SourceHttpMessageConverter@1369} 4 = {AllEncompassingFormHttpMessageConverter@1370} 5 = {Jaxb2RootElementHttpMessageConverter@1371} 6 = {MappingJackson2HttpMessageConverter@1372} – Zack Macomber Feb 05 '16 at 17:59
  • can you post your `Users` class – Jaiwo99 Feb 05 '16 at 18:41
  • @Jaiwo99 - I've been researching and it looks like my `User` class is indeed the issue - it's a big monster. Is there a way to make Jackson more lenient in it's serialization/deserialization? I guess I could also modify the `User` class to be a solid POJO that Jackson can handle – Zack Macomber Feb 05 '16 at 19:16
  • yes, sure, you can.. like @JsonIgnore or so.. – Jaiwo99 Feb 07 '16 at 22:24

1 Answers1

2

Turned out to be an issue in my Users class (more specifically the User class in List<User>).

I updated the User class with @JsonIgnore on fields that I thought might be causing the Exception and I was able to get passed this issue.

So for others that might encounter this issue, check the object you're trying to do a getForObject on to make sure everything can map fine.

Zack Macomber
  • 6,682
  • 14
  • 57
  • 104