如何使用RestTemplate获取结果并将响应放入List中?

huangapple go评论69阅读模式
英文:

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 in my function using RestTemplate, here is how my code look like :

@PostMapping("/save-new")
public ResponseEntity saveNewShipmentAddressGrouping(@Valid @RequestBody InputRequest 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 into 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?"

英文:

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(&quot;/save-new&quot;)
public ResponseEntity&lt;ShipmentAddressGrouping&gt; saveNewShipmentAddressGrouping(@Valid @RequestBody InputRequest&lt;ShipmentAddressGroupingDto&gt; request) {
    String url = baseUrl + &quot;/load-list&quot;;

    HttpEntity&lt;PartnerShipmentDto&gt; requestPartnerShipmentDto = new HttpEntity&lt;&gt;(new PartnerShipmentDto());
    RestTemplate restTemplate = new RestTemplate();
    List&lt;PartnerShipmentDto&gt; partnerShipmentDto = restTemplate.postForObject(url, requestPartnerShipmentDto, new ParameterizedTypeReference&lt;List&lt;PartnerShipmentDto&gt;&gt;() {});

    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&lt;List&lt;PartnerShipmentDto&gt;&gt;() {});

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&lt;T&gt;,您需要使用RestTemplate.exchange(),因为这是唯一公开暴露ParameterizedTypeReference&lt;T&gt;类型参数的方法。

List&lt;PartnerShipmentDto&gt; partnerShipmentDto = restTemplate.exchange(url, 
                    HttpMethod.GET, 
                    requestPartnerShipmentDto,
                    new ParameterizedTypeReference&lt;List&lt;PartnerShipmentDto&gt;&gt;() {})
            .getBody();
英文:

If you want to use ParameterizedTypeReference&lt;T&gt;, you need to use RestTemplate.exchange(), since this is the only Method that exposes a parameter of type ParameterizedTypeReference&lt;T&gt;.

List&lt;PartnerShipmentDto&gt; partnerShipmentDto = restTemplate.exchange(url, 
                HttpMethod.GET, 
                requestPartnerShipmentDto,
                new ParameterizedTypeReference&lt;List&lt;PartnerShipmentDto&gt;&gt;() {})
        .getBody();

huangapple
  • 本文由 发表于 2020年8月6日 18:34:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63281734.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定