Search code examples
javaspringspring-restcontroller

no suitable HttpMessageConverter found for response type [class com.avada.rest.UsersController$Users]


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


Solution

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