找不到适合响应类型 [class com.avada.rest.UsersController$Users] 的 HttpMessageConverter

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

我收到以下异常,但不确定原因...

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)

这是我的 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;
        }
    }
}

这是我的测试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());
    }
}

如果我注释掉 "Get all users" 的测试代码,我可以让单个用户没问题。

我在这段代码中做错了什么?

P.S。 - 我可以通过浏览器调用 getUsers(),json 返回正常,所以我知道 getUsers() 正在工作...只是无法让 RestTemplate 到工作

原来是我的 Users class 中的一个问题(更具体地说 List<User> 中的 User class)。

我在我认为可能导致异常的字段上用 @JsonIgnore 更新了 User class,并且我能够解决这个问题。

因此,对于可能遇到此问题的其他人,请检查您尝试对其执行 getForObject 的对象,以确保一切都可以正常映射。