RestTemplate restTemplate = new RestTemplate(); String apiUrl = "http://10.79.7.175:9000/aip/eam/equip/getEquipRouterList"; EquipRequestVo equipRequestVo = new EquipRequestVo().setSysCode("TCAR").setPkCategory("NHZC3100010000110000").setPkOrg("12").setPageRequest(new EquipRequestVo.PageParam().setPageNum(1).setPageSize(500)); ResponseEntity<EquipEntity> responseEntity = restTemplate.postForEntity(apiUrl, equipRequestVo, EquipEntity.class); EquipEntity equip = responseEntity.getBody(); System.out.println(equip);,将代码改为post请求,以json格式发送body,且接收List对象
时间: 2024-04-11 20:31:35 浏览: 177
好的,你可以尝试使用以下代码将原有的代码改为发送 POST 请求并以 JSON 格式发送请求体,并接收一个 List 对象的返回值:
```java
RestTemplate restTemplate = new RestTemplate();
String apiUrl = "http://10.79.7.175:9000/aip/eam/equip/getEquipRouterList";
// 设置请求头
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
// 设置请求体
EquipRequestVo equipRequestVo = new EquipRequestVo()
.setSysCode("TCAR")
.setPkCategory("NHZC3100010000110000")
.setPkOrg("12")
.setPageRequest(new EquipRequestVo.PageParam().setPageNum(1).setPageSize(500));
// 发送 POST 请求
HttpEntity<EquipRequestVo> requestEntity = new HttpEntity<>(equipRequestVo, headers);
ResponseEntity<List<EquipEntity>> responseEntity = restTemplate.exchange(apiUrl, HttpMethod.POST, requestEntity, new ParameterizedTypeReference<List<EquipEntity>>() {});
// 获取响应体
List<EquipEntity> equipList = responseEntity.getBody();
System.out.println(equipList);
```
这样,你就可以通过 `equipList` 变量获取返回的 List 对象了。
阅读全文