V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Vimax
V2EX  ›  Java

restTemplate.getForEntity 怎么返回匹配 ResponseEntity<List<Payment>> 的类型对象

  •  1
     
  •   Vimax · 2020-06-15 18:21:55 +08:00 · 2294 次点击
    这是一个创建于 1408 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我想返回ResponseEntity<List<Payment>>的格式类型,如何在保持泛型的同时List<Payment>,使用restTemplate.getForEntity返回正确的数据类型。

    下面的代码是错误的,不兼容的返回类型:

      @GetMapping
        public ResponseEntity<List<Payment>> getPayment(Payment payment) {
            return restTemplate.getForEntity(SERVERURL + "/payment", List.class);
        }
    

    提示信息:

    Required type:
    ResponseEntity <List<Payment>>
    
    Provided:
    ResponseEntity<List>
    
    Incompatible equality constraint: List<Payment> and List
    

    将返回类型去掉泛型,返回类型为 ResponseEntity<List>,可以解决兼容问题并返回数据。

      @GetMapping
        public ResponseEntity<List> getPayment(Payment payment) {
            return restTemplate.getForEntity(SERVERURL + "/payment", List.class);
        }
    

    但是如何做,才能返回类型匹配 ResponseEntity<List<Payment>>。

      @GetMapping
        public ResponseEntity<List<Payment>> getPayment(Payment payment) {
            List<Payment> list = new ArrayList<>();
            return restTemplate.getForEntity(SERVERURL + "/payment", list.getClass());
        }
       
    

    错误提示:

    Required type:
    ResponseEntity <List<Payment>>
    Provided:
    ResponseEntity <capture of ? extends List>
    Incompatible equality constraint: List<Payment> and capture of ? extends List
    
    第 1 条附言  ·  2020-06-15 19:48:32 +08:00
    采用 1 楼和 2 楼的答案。

    ```java
    // extend ArrayList<Payment>
    // return restTemplate.getForEntity(SERVERURL + "/payment", PaymentList.class);
    return restTemplate.exchange(SERVERURL, HttpMethod.GET, null, new ParameterizedTypeReference<List<Payment>>() {
    });
    ```
    第 2 条附言  ·  2020-06-16 09:32:28 +08:00
    追加采纳 5 楼的答案:

    使用数组对象封装返回结果集。
    8 条回复    2020-06-21 23:18:06 +08:00
    chendy
        1
    chendy  
       2020-06-15 19:00:00 +08:00   ❤️ 2
    写一个 public class X extens ArrayList<Payment> ,然后参数里放 X.class
    或者用 ParameterizedTypeReference<List<Payment>>,但是有点长而且只能用 exchange
    nthin0
        2
    nthin0  
       2020-06-15 19:06:23 +08:00   ❤️ 1
    restTemplate.exchange(requestEntity, new ParameterizedTypeReference<List<Payment>>() {});
    Vimax
        3
    Vimax  
    OP
       2020-06-15 19:45:59 +08:00
    @chendy 谢谢提供两种思路,采纳。
    Vimax
        4
    Vimax  
    OP
       2020-06-15 19:46:13 +08:00
    @nthin0 很棒,采纳!
    hantsy
        5
    hantsy  
       2020-06-15 20:17:35 +08:00   ❤️ 1
    也可以用数组 payment[] 封装结果 。
    Vimax
        6
    Vimax  
    OP
       2020-06-16 09:31:28 +08:00
    @hantsy 谢谢,另类思路。追加采纳!
    kennylam777
        7
    kennylam777  
       2020-06-20 18:54:52 +08:00
    exchange +1 , getForEntity 對 Map/List 封裝就是個坑
    siweipancc
        8
    siweipancc  
       2020-06-21 23:18:06 +08:00 via iPhone
    :D 一直用 exchange,没发现这个问题,受教了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3325 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 12:13 · PVG 20:13 · LAX 05:13 · JFK 08:13
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.