英文:
How to use RestTemplate to get result in List and put the response into List?
问题
以下是您要翻译的部分:
"Hi what i trying to achieve is i want to consume other API and put some response data into List
@PostMapping("/save-new")
public ResponseEntity
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);
}
as you can see i try to get the response into ListrestTemplate.postForObject(url, requestPartnerShipmentDto, new ParameterizedTypeReference<List<PartnerShipmentDto>>() {});
but i got error underlined in restTemplate.postForObject that look like this :
The method postForObject(String, Object, Class
, Object...) in the
type RestTemplate is not applicable for the arguments (String,
HttpEntity, new
ParameterizedTypeReference<List>() {})
What should i change to fix this?"
英文:
Hi what i trying to achieve is i want to consume other API and put some response data into List<myObject> in my function using RestTemplate, here is how my code look like :
@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);
}
as you can see i try to get the response in to List<PartnerShipmentDto>, which is i try here restTemplate.postForObject(url, requestPartnerShipmentDto, new ParameterizedTypeReference<List<PartnerShipmentDto>>() {});
but i got error underlined in restTemplate.postForObject that look like this :
> The method postForObject(String, Object, Class<T>, Object...) in the
> type RestTemplate is not applicable for the arguments (String,
> HttpEntity<PartnerShipmentDto>, new
> ParameterizedTypeReference<List<PartnerShipmentDto>>(){})
What should i change to fix this?
答案1
得分: 2
如果您想使用ParameterizedTypeReference<T>
,您需要使用RestTemplate.exchange()
,因为这是唯一公开暴露ParameterizedTypeReference<T>
类型参数的方法。
List<PartnerShipmentDto> partnerShipmentDto = restTemplate.exchange(url,
HttpMethod.GET,
requestPartnerShipmentDto,
new ParameterizedTypeReference<List<PartnerShipmentDto>>() {})
.getBody();
英文:
If you want to use ParameterizedTypeReference<T>
, you need to use RestTemplate.exchange()
, since this is the only Method that exposes a parameter of type ParameterizedTypeReference<T>
.
List<PartnerShipmentDto> partnerShipmentDto = restTemplate.exchange(url,
HttpMethod.GET,
requestPartnerShipmentDto,
new ParameterizedTypeReference<List<PartnerShipmentDto>>() {})
.getBody();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论