类 ResponseBodyEmitter
- java.lang.Object
- org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter
- 直接已知子类:
SseEmitter
public class ResponseBodyEmitter extends Object
A controller method return value type for asynchronous request processing where one or more objects are written to the response.While
DeferredResult
is used to produce a single result, aResponseBodyEmitter
can be used to send multiple objects where each object is written with a compatibleHttpMessageConverter
.Supported as a return type on its own as well as within a
ResponseEntity
.@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();
- 从以下版本开始:
- 4.2
- 作者:
- Rossen Stoyanchev, Juergen Hoeller
嵌套类概要
嵌套类 修饰符和类型 类 说明 static class
ResponseBodyEmitter.DataWithMediaType
A simple holder of data to be written along with a MediaType hint for selecting a message converter to write with.
构造器概要
构造器 构造器 说明 ResponseBodyEmitter()
Create a new ResponseBodyEmitter instance.ResponseBodyEmitter(Long timeout)
Create a ResponseBodyEmitter with a custom timeout value.
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 void
complete()
Complete request processing.void
completeWithError(Throwable ex)
Complete request processing with an error.protected void
extendResponse(ServerHttpResponse outputMessage)
Invoked after the response is updated with the status code and headers, if the ResponseBodyEmitter is wrapped in a ResponseEntity, but before the response is committed, i.e. before the response body has been written to.Long
getTimeout()
Return the configured timeout value, if any.void
onCompletion(Runnable callback)
Register code to invoke when the async request completes.void
onTimeout(Runnable callback)
Register code to invoke when the async request times out.void
send(Object object)
Write the given object to the response.void
send(Object object, MediaType mediaType)
Write the given object to the response also using a MediaType hint.
构造器详细资料
ResponseBodyEmitter
public ResponseBodyEmitter()
Create a new ResponseBodyEmitter instance.
ResponseBodyEmitter
public ResponseBodyEmitter(Long timeout)
Create a ResponseBodyEmitter with a custom timeout value.By default not set in which case the default configured in the MVC Java Config or the MVC namespace is used, or if that's not set, then the timeout depends on the default of the underlying server.
- 参数:
timeout
- timeout value in milliseconds
方法详细资料
getTimeout
public Long getTimeout()
Return the configured timeout value, if any.
extendResponse
protected void extendResponse(ServerHttpResponse outputMessage)
Invoked after the response is updated with the status code and headers, if the ResponseBodyEmitter is wrapped in a ResponseEntity, but before the response is committed, i.e. before the response body has been written to.The default implementation is empty.
send
public void send(Object object) throws IOException
Write the given object to the response.If any exception occurs a dispatch is made back to the app server where Spring MVC will pass the exception through its exception handling mechanism.
- 参数:
object
- the object to write- 抛出:
IOException
- raised when an I/O error occursIllegalStateException
- wraps any other errors
send
public void send(Object object, MediaType mediaType) throws IOException
Write the given object to the response also using a MediaType hint.If any exception occurs a dispatch is made back to the app server where Spring MVC will pass the exception through its exception handling mechanism.
- 参数:
object
- the object to writemediaType
- a MediaType hint for selecting an HttpMessageConverter- 抛出:
IOException
- raised when an I/O error occursIllegalStateException
- wraps any other errors
complete
public void complete()
Complete request processing.A dispatch is made into the app server where Spring MVC completes asynchronous request processing.
Note: you do not need to call this method after an
IOException
from any of thesend
methods. The Servlet container will generate an error notification that Spring MVC will process and handle through the exception resolver mechanism and then complete.
completeWithError
public void completeWithError(Throwable ex)
Complete request processing with an error.A dispatch is made into the app server where Spring MVC will pass the exception through its exception handling mechanism.
onTimeout
public void onTimeout(Runnable callback)
Register code to invoke when the async request times out. This method is called from a container thread when an async request times out.
onCompletion
public void onCompletion(Runnable callback)
Register code to invoke when the async request completes. This method is called from a container thread when an async request completed for any reason including timeout and network error. This method is useful for detecting that aResponseBodyEmitter
instance is no longer usable.