Hi what i trying to achieve is i want to consume other API and put some response data into List 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, 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, Object...) in the type RestTemplate is not applicable for the arguments (String, HttpEntity, new ParameterizedTypeReference<List>(){})
What should i change to fix this?
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();