我正在创建一个应用程序来读取事件流,并将信息保留在我的数据库中。
我有一个网址:http://xx.xx.xx.xx:8080/restconf/streams/mno-vnf-event/json
所需的标题:Accept:text/event-stream
,Authorization:Basic YWRtaW46o99RtaW4-
,Content-Type:application/yang-data+json
现在,我想使用Java(不是javascript代码)从上述API提取流,任何人都有实现此想法的想法。
答案 0 :(得分:0)
您可以使用一些支持SSE的Java库:
这些是我想到的第一个,但无疑还有更多。
答案 1 :(得分:0)
如果SSE服务器严格遵循SSE规范,则无法添加任何自定义标头。
尽管如此,您是否使用Spring WebFlux Client尝试了类似的操作:
ResolvableType type = forClassWithGenerics(ServerSentEvent.class, String.class);
WebClient client = WebClient.create();
client.get()
.uri("http://localhost:8080/sse")
.accept(TEXT_EVENT_STREAM)
.header("MyHeader", "Foo") // Add your headers here.
.exchange()
.flatMapMany(response -> response.body(toFlux(type)))
.subscribe(System.out::println,
Throwable::printStackTrace);
我已经针对SSE Undertow服务器测试了这段代码。似乎可行:Undertow能够获取已发送的自定义标头。