将列表作为参数传递给Java中的方法

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

Passing List as Parameter in Java

问题

import org.springframework.web.util.UriComponentsBuilder;
import java.net.URI;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

// ... (Other code and imports)

List<Attachments> attList = /* Your list of Attachments here */;
Map<String, String> attachmentMap = new HashMap<>();

Optional.ofNullable(attList)
    .orElse(Collections.emptyList())
    .forEach(aList -> {
        attachmentMap.put("id", aList.getId());
        LOGGER.debug("Attachment ids : {}", attachmentMap);
        // Build and execute API call for each attachment ID
        URI uri = UriComponentsBuilder
            .fromPathSegment("attachments")
            .pathSegment(aList.getId())
            .pathSegment("retrieve")
            .build()
            .toUri();
        LOGGER.debug("URL to Retrieve: {}", uri.toString());
        // Now you can use the 'uri' to make the API call for this attachment
        // ...
    });

Please note that the above code is just a snippet and needs to be integrated into your existing codebase. Also, make sure to replace the comments with the appropriate code for making the API call using the constructed URI.

英文:

I have a list this.

[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)]

I need to iterate and get ids from this list and pass each id as parameter for an API call - attachments/{{attachmentid}}/retrieve .

I am able to retrieve ids and store them in a Map . (Not sure if using Map is correct here?)
This is the code snippet I have written , But I am unable to proceed after that.

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

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

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

I am able to print attachment ids - but how do I pass each of them as parameter to an api call like this - attachments/{{attachmentid}}/retrieve

答案1

得分: 0

请使用foreach循环遍历映射并调用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(String id) {
    URI uri = UriComponentsBuilder
            .pathSegment(ATTACHMENTS)
            .pathSegment(id)   //在这里放什么?
            .pathSegment(RETRIEVE)
            .build().toUri();
    LOGGER.debug("URL to Retrieve  : {}", uri.toString());
}
英文:

Please use foreach to iterate through map and call the http call method

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

create Http call method with parameter id(also other params you need) and use method to call.

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

答案2

得分: 0

直接循环遍历 Attachments 列表,从每个 Attachment 中获取 id,并将其传递给 UriComponentsBuilder 以构建 URI。无需额外的数据结构来保存 Ids。

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); // 或者您可以直接在此处使用您的 http 客户端调用 uri。
    }
}
英文:

Why not just loop over Attachments list directly and retrieve the id from each Attachment and pass it to the UriComponentsBuilder to build the URI. You don't need an extra data structure to hold the Ids.

List&lt;URI&gt; uriList = new ArrayList&lt;&gt;();

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.
   }
}

huangapple
  • 本文由 发表于 2020年8月28日 01:45:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63621556.html
匿名

发表评论

匿名网友

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

确定