如何使用RestTemplate获取List <object>(SpringBoot)

时间:2017-11-07 13:09:08

标签: list spring-boot resttemplate

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

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

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

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

问题:如何使用其他模板获取List<User>

1 个答案:

答案 0 :(得分:1)

也许你想尝试这种方法:

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