resttemplate 返回list
时间: 2023-09-12 18:05:41 浏览: 72
restTemplate返回List的示例代码如下:
```java
@GetMapping("/test-list-all")
public List<User> listAll() {
ParameterizedTypeReference<List<User>> responseType = new ParameterizedTypeReference<List<User>>() {};
ResponseEntity<List<User>> responseEntity = restTemplate.exchange("http://microservice-provider-user/list-all", HttpMethod.GET, null, responseType);
List<User> userList = responseEntity.getBody();
for (User user : userList) {
System.out.println(user.getId());
}
return userList;
}
```
在这个示例中,我们使用了`ParameterizedTypeReference`来指定返回类型为List<User>。然后使用`restTemplate.exchange`方法发送GET请求,并将返回结果赋值给`ResponseEntity<List<User>>`。通过`getBody`方法获取具体的List<User>对象,并进行后续的处理。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [restTemplate](https://blog.csdn.net/u013536313/article/details/90287180)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [RestTemplate请求的服务实例返回List类型,用数组接收](https://blog.csdn.net/horse_well/article/details/88879185)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文