英文:
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<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 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("URL to Retrieve : {}", 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<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);
});
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("URL to Retrieve : {}", 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<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.
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论