我正在使用Jersey 2.28实现服务器发送事件,但我无法使用基本示例。
其他答案提到了一个依赖问题,我已经添加了所有必要的依赖,但无济于事:
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-grizzly2-http</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-grizzly2-servlet</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-sse</artifactId>
</dependency>
</dependencies>
我基本上复制并粘贴了示例,然后运行它,我尝试了在https://www.baeldung.com/java-ee-jax-rs-sse等教程中找到的变体,但这只是行不通
@GET
@Path("/locations")
@Produces(MediaType.SERVER_SENT_EVENTS)
public void getServerSentEvents(@Context SseEventSink eventSink, @Context Sse sse) {
new Thread(() -> {
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
final OutboundSseEvent event = sse.newEventBuilder().name("message-to-client")
.data(String.class, "Hello world " + i + "!").build();
eventSink.send(event);
}
}).start();
}
任何人都可以使用Jersey为我提供基本SSE的正确实现(我使用的是灰熊的实现)