如何使用 RestTemplate 在 List 中获取结果并将响应放入 List?
How to use RestTemplate to get result in List and put the response into List?
嗨,我想要实现的是我想使用其他 API 并使用 RestTemplate 将一些响应数据放入我的函数中的列表中,这是我的代码的样子:
@PostMapping("/save-new")
public ResponseEntity<ShipmentAddressGrouping> saveNewShipmentAddressGrouping(@Valid @RequestBody InputRequest<ShipmentAddressGroupingDto> request) {
String url = baseUrl + "/load-list";
HttpEntity<PartnerShipmentDto> requestPartnerShipmentDto = new HttpEntity<>(new PartnerShipmentDto());
RestTemplate restTemplate = new RestTemplate();
List<PartnerShipmentDto> partnerShipmentDto = restTemplate.postForObject(url, requestPartnerShipmentDto, new ParameterizedTypeReference<List<PartnerShipmentDto>>() {});
ShipmentAddressGrouping newShipmentAddressGrouping = shipmentAddressGroupingService.save(request);
return ResponseEntity.ok(newShipmentAddressGrouping);
}
如您所见,我尝试将响应放入 List,这是我在这里尝试的 restTemplate.postForObject(url, requestPartnerShipmentDto, new ParameterizedTypeReference<List<PartnerShipmentDto>>() {});
但我在 restTemplate.postForObject 中看到错误,如下所示:
The method postForObject(String, Object, Class, Object...) in the
type RestTemplate is not applicable for the arguments (String,
HttpEntity, new
ParameterizedTypeReference<List>(){})
我应该做些什么来解决这个问题?
如果你想使用ParameterizedTypeReference<T>
,你需要使用RestTemplate.exchange()
,因为这是唯一公开ParameterizedTypeReference<T>
.
类型参数的方法
List<PartnerShipmentDto> partnerShipmentDto = restTemplate.exchange(url,
HttpMethod.GET,
requestPartnerShipmentDto,
new ParameterizedTypeReference<List<PartnerShipmentDto>>() {})
.getBody();
嗨,我想要实现的是我想使用其他 API 并使用 RestTemplate 将一些响应数据放入我的函数中的列表中,这是我的代码的样子:
@PostMapping("/save-new")
public ResponseEntity<ShipmentAddressGrouping> saveNewShipmentAddressGrouping(@Valid @RequestBody InputRequest<ShipmentAddressGroupingDto> request) {
String url = baseUrl + "/load-list";
HttpEntity<PartnerShipmentDto> requestPartnerShipmentDto = new HttpEntity<>(new PartnerShipmentDto());
RestTemplate restTemplate = new RestTemplate();
List<PartnerShipmentDto> partnerShipmentDto = restTemplate.postForObject(url, requestPartnerShipmentDto, new ParameterizedTypeReference<List<PartnerShipmentDto>>() {});
ShipmentAddressGrouping newShipmentAddressGrouping = shipmentAddressGroupingService.save(request);
return ResponseEntity.ok(newShipmentAddressGrouping);
}
如您所见,我尝试将响应放入 List,这是我在这里尝试的 restTemplate.postForObject(url, requestPartnerShipmentDto, new ParameterizedTypeReference<List<PartnerShipmentDto>>() {});
但我在 restTemplate.postForObject 中看到错误,如下所示:
The method postForObject(String, Object, Class, Object...) in the type RestTemplate is not applicable for the arguments (String, HttpEntity, new ParameterizedTypeReference<List>(){})
我应该做些什么来解决这个问题?
如果你想使用ParameterizedTypeReference<T>
,你需要使用RestTemplate.exchange()
,因为这是唯一公开ParameterizedTypeReference<T>
.
List<PartnerShipmentDto> partnerShipmentDto = restTemplate.exchange(url,
HttpMethod.GET,
requestPartnerShipmentDto,
new ParameterizedTypeReference<List<PartnerShipmentDto>>() {})
.getBody();