SSE implementation in Spring REST

房东的猫 提交于 2019-12-07 09:35:34

问题


Can anybody provide an example for SSE ( Server sent events) using Spring Rest ? Basically i have a request and the response for it would be sent by the server in multiple chunks. I would like to have the server and client implementation in Spring REST Api without third party rest api like jersey.


回答1:


There isn't any direct support for SSE in Spring currently but it looks like it will be available in 4.2 which is in RC2 right now You can see the details here https://jira.spring.io/browse/SPR-12212

This works via returning an SseEmitter or a ResponseBodyEmitter from the controller methods.

@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();

You can see the Reference documentation here http://docs.spring.io/spring/docs/4.2.0.RC2/spring-framework-reference/htmlsingle/#mvc-ann-async-http-streaming



来源:https://stackoverflow.com/questions/31229015/sse-implementation-in-spring-rest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!