如何使用 RestTemplate(Spring Boot) 获取 List<Object>

How to get List<Object> with RestTemplate(SpringBoot)

我想得到:List<User>。我有用于获取用户的端点(GET)。它得到:

[
  {
    "id": "d71dcbca-54f3-4b19-aec4-3776bfe34730",
    "name": "test",
    "surname": "test",
    "login": "test",
    "password": "-26104458",
    "email": "test",
    "role": "user"
  }
]

我尝试使用其余模板获取此列表:

ResponseEntity<User[]> responseEntity = rest.getForEntity(my-endpoint, User[].class);
        return Arrays.asList(responseEntity.getBody());

但是我得到这个错误:

org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type

问题:如何使用 rest 模板获得 List<User>

也许您想尝试这种方法:

ResponseEntity<List<User>> responseEntity = rest.exchange(
                "your-endpoint",
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<List<User>>() {
                });

另见 https://docs.spring.io/spring/docs/4.3.12.RELEASE/spring-framework-reference/htmlsingle/#rest-resttemplate