Akka:自定义httpheader,将hashmap转换为Itable< httpheader>在爪哇
我正在尝试在Akka生成自定义Httpheader。我有一个标题条目的示意图,我需要转换为峰值。 这是我的代码:
import akka.http.javadsl.model.HttpHeader;
HashMap<String, String> headersMap = new HashMap<>();
headersMap.put("key1","value1");
return (Iterable<HttpHeader>) headersMap;
//HttpRequest.create().withUri("uri").addHeaders(Iterable<HttpHeader> iterable)
public static Iterable<HttpHeader> convertToRecordHttpHeaders(Map<String, String> headersMap){
return headersMap.entrySet().stream()
.map(x -> new HttpHeader(x.getKey(), x.getValue()))
.collect(Collectors.toList());
}
将地图转换为httpheader并将其转换为迭代的有效方法是什么,因为我想在akka中创建httprequest。
解决方案: 使用Rawheader添加标头以要求
request.addHeader(RawHeader.create("key","value"));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(2)
梦亿 2025-01-25 11:42:25
假设您的 Header 类是这样的
class HttpHeader{
public HttpHeader(Map.Entry<String,String> header){}
}
,然后只需使用构造函数引用代替 lambda,否则您可以使用 lambda 表达式
headersMap.entrySet().stream().map(HttpHeader::new).iterator 提供解析();
但我仍然不明白为返回 HttpHeader 迭代器创建 HashMap 的用例。
我猜你曾经保持标头键唯一的viva映射,但是你可以使用TreeSet来维护唯一的对象
class HttpHeader implements Comparable<HttpHeader>{
String key;
String value;
public HttpHeader(String key, String value){
this.key = key;
this.value = value;
}
@Override
public int compareTo(HttpHeader o) {
return key.compareTo(o.key);
}
}
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
HashMap
不是一个Iterable
任何东西。它的entrySet()
、keySet()
和values()
是,但对于您的地图来说,这些是Iterable
Iterable
分别是 String、String>>
、Iterable
和Iterable
。这意味着您无法以任何方式将映射转换为Iterable
。您可以做的是转换:
这假设
HttpHeader
有一个采用标头名称和值的构造函数。如果没有,请将构造函数调用替换为创建HttpHeader
的其他内容。A
HashMap
is not anIterable
anything. ItsentrySet()
,keySet()
andvalues()
are, but for your map those areIterable<Map.Entry<String, String>>
,Iterable<String>
andIterable<String>
respectively. That means that you cannot cast the map in any way to anIterable<HttpHeader>
.What you can do is convert:
This assumes that
HttpHeader
has a constructor that takes a header name and value. If it doesn't, replace the constructor call with something else that creates anHttpHeader
.