Spring REST 中的 SSE 实现

SSE implementation in Spring REST

任何人都可以使用 Spring Rest 提供 SSE(服务器发送事件)的示例吗?基本上我有一个请求,它的响应将由服务器以多个块的形式发送。我想在 Spring REST Api 中实现服务器和客户端,而无需第三方 rest api 像球衣。

目前 Spring 中没有对 SSE 的任何直接支持,但看起来它将在 RC2 中的 4.2 中可用 您可以在此处查看详细信息 https://jira.spring.io/browse/SPR-12212

这通过从控制器方法返回 SseEmitter 或 ResponseBodyEmitter 来实现。

@RequestMapping(value="/stream", method=RequestMethod.GET)
public ResponseBodyEmitter handle() {
        ResponseBodyEmitter emitter = new ResponseBodyEmitter();
        // Pass the emitter to another component...
        return emitter;
}

// in another thread
emitter.send(foo1);

// and again
emitter.send(foo2);

// and done
emitter.complete();

您可以在此处查看参考文档 http://docs.spring.io/spring/docs/4.2.0.RC2/spring-framework-reference/htmlsingle/#mvc-ann-async-http-streaming