【问题标题】:Passing List as Parameter in Java在 Java 中将列表作为参数传递
【发布时间】:2020-08-28 01:57:28
【问题描述】:

我有一个清单。

[Attachments(filename=a.json,  id=ATT-mXRJB-BmVzs, contentType=application/json),
 Attachments(filename=b.pdf,  id=ATT-y7Qr2-8RqkW, contentType=application/pdf ),
 Attachments(filename=c.docx,  id=ATT-mYh3z-3YJ37, contentType=application/vnd.openxmlformats-officedocument.wordprocessingml.document)]

我需要迭代并从此列表中获取 id,并将每个 id 作为 API 调用的参数传递 - attachments/{{attachmentid}}/retrieve 。

我能够检索 id 并将它们存储在 Map 中。 (不确定在这里使用 Map 是否正确?) 这是我写的sn-p代码,但是之后我就无法继续了。

  Map<String, String> attachmentMap = new HashMap<String, String>();
    Optional.ofNullable(attList)
                .orElse(Collections.emptyList())
                .forEach(aList -> {
                     attachmentMap.put("id", aList.getId());
                    LOGGER.debug("Attachment ids : {}",attachmentMap);
                });
    

        

2020-08-27 22:21:49.967 调试 18644 --- [nio-8081-exec-2] c.f.p.h.s.i.PlServiceImpl:附件 ID:{id=ATT-mXRJB-BmVzs} 2020-08-27 22:21:49.967 调试 18644 --- [nio-8081-exec-2] c.f.p.h.s.i.PlServiceImpl:附件 ID:{id=ATT-y7Qr2-8RqkW} 2020-08-27 22:21:49.967 调试 18644 --- [nio-8081-exec-2] c.f.p.h.s.i.PlServiceImpl:附件 ID:{id=ATT-mYh3z-3YJ37}

  URI uri = UriComponentsBuilder.
                    .pathSegment(ATTACHMENTS)
                    .pathSegment(?)   //what to give here? 
                    .pathSegment(RETRIEVE)
                    .build().toUri();
        LOGGER.debug("URL to Retrieve  : {}", uri.toString());

我能够打印附件 ID - 但是如何将它们作为参数传递给这样的 api 调用 - attachments/{{attachmentid}}/retrieve

【问题讨论】:

    标签: java arrays arraylist


    【解决方案1】:

    请使用foreach遍历map并调用http调用方法

    Map<String, String> attachmentMap = new HashMap<String, String>();
        Optional.ofNullable(attList)
                    .orElse(Collections.emptyList())
                    .forEach(aList -> {
                         attachmentMap.put("id", aList.getId());
                         httpCall(aList.getId());
                        LOGGER.debug("Attachment ids : {}",attachmentMap);
                    });
    

    创建带有参数 id 的 Http 调用方法(以及您需要的其他参数)并使用方法调用。

    public static void httpCall(id){
      URI uri = UriComponentsBuilder.
                        .pathSegment(ATTACHMENTS)
                        .pathSegment(id)   //what to give here? 
                        .pathSegment(RETRIEVE)
                        .build().toUri();
            LOGGER.debug("URL to Retrieve  : {}", uri.toString());
    }
    

    【讨论】:

      【解决方案2】:

      为什么不直接循环 Attachments 列表并从每个 Attachment 检索 id 并将其传递给 UriComponentsBuilder 以构建 URI。您不需要额外的数据结构来保存 Id。

      List<URI> uriList = new ArrayList<>();
      
      for(Attachment attachment: Attachments){
         String id = attachment.getId();
         if (id != null) {
             URI uri = UriComponentsBuilder.
                      .pathSegment(ATTACHMENTS)
                      .pathSegment(id)
                      .pathSegment(RETRIEVE)
                      .build().toUri();
             uriList.add(uri); // or you can call the uri directly from here using your http client.
         }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-11-13
        • 2015-04-17
        • 2011-01-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-04
        • 2013-05-23
        相关资源
        最近更新 更多