Top Description Fields Constructors Methods
org.springframework.http.codec

public Class ServerSentEventHttpMessageWriter

extends Object
implements HttpMessageWriter<Object>
Class Inheritance
All Implemented Interfaces
org.springframework.http.codec.HttpMessageWriter
Imports
java.nio.charset.StandardCharsets, java.time.Duration, java.util.Collections, .List, .Map, org.apache.commons.logging.Log, org.reactivestreams.Publisher, reactor.core.publisher.Flux, .Mono, org.springframework.core.ResolvableType, org.springframework.core.codec.CodecException, .Encoder, .Hints, org.springframework.core.io.buffer.DataBuffer, .DataBufferFactory, .DataBufferUtils, org.springframework.http.HttpLogging, .MediaType, .ReactiveHttpOutputMessage, org.springframework.http.server.reactive.ServerHttpRequest, .ServerHttpResponse, org.springframework.lang.Nullable, org.springframework.util.Assert, .StringUtils

HttpMessageWriter for "text/event-stream" responses.
Authors
Sebastien Deleuze, Arjen Poutsma, Rossen Stoyanchev
Since
5.0

Field Summary

Modifier and TypeField and Description
private static final MediaType
private final Encoder<?>
private static final Log
private static final List<MediaType>

Constructor Summary

AccessConstructor and Description
public
ServerSentEventHttpMessageWriter()

Constructor without an Encoder.

public
ServerSentEventHttpMessageWriter(Encoder<?>
the Encoder to use (may be null)
encoder
)

Constructor with JSON Encoder for encoding objects.

Method Summary

Modifier and TypeMethod and Description
public boolean
canWrite(ResolvableType
the type of object to check
elementType
,
MediaType
the media type for the write (possibly null)
mediaType
)

Implements org.springframework.http.codec.HttpMessageWriter.canWrite.

Whether the given object type is supported by this writer.
private Flux<Publisher<DataBuffer>>
encode(Publisher<?> input, ResolvableType elementType, MediaType mediaType, DataBufferFactory factory, Map<String, Object> hints)

private <T> Flux<DataBuffer>
encodeEvent(StringBuilder eventContent, T data, ResolvableType dataType, MediaType mediaType, DataBufferFactory factory, Map<String, Object> hints)

private DataBuffer
encodeText(CharSequence text, MediaType mediaType, DataBufferFactory bufferFactory)

private Map<String, Object>
getEncodeHints(ResolvableType actualType, ResolvableType elementType, MediaType mediaType, ServerHttpRequest request, ServerHttpResponse response)

public Encoder<?>
getEncoder()

Return the configured Encoder, if any.

public List<MediaType>
getWritableMediaTypes()

Implements org.springframework.http.codec.HttpMessageWriter.getWritableMediaTypes.

Return the list of media types supported by this Writer.
public Mono<Void>
write(Publisher<?>
the objects to write
input
,
ResolvableType
the type of objects in the stream which must have been previously checked via canWrite(ResolvableType, MediaType)
elementType
,
MediaType
the content type for the write (possibly null to indicate that the default content type of the writer must be used)
mediaType
,
ReactiveHttpOutputMessage
the message to write to
message
,
Map<String, Object>
additional information about how to encode and write
hints
)

Implements org.springframework.http.codec.HttpMessageWriter.write.

Write a given stream of object to the output message.
public Mono<Void>
write(Publisher<?> input, ResolvableType
the actual return type of the method that returned the value; for annotated controllers, the MethodParameter can be accessed via ResolvableType#getSource().
actualType
,
ResolvableType
the type of Objects in the input stream
elementType
,
MediaType
the content type to use (possibly null indicating the default content type of the writer should be used)
mediaType
,
ServerHttpRequest
the current request
request
,
ServerHttpResponse
the current response
response
,
Map<String, Object> hints)

Overrides default org.springframework.http.codec.HttpMessageWriter.write.

Server-side only alternative to write(Publisher, ResolvableType, MediaType, ReactiveHttpOutputMessage, Map) with additional context available.
private void
writeField(String fieldName, Object fieldValue, StringBuilder sb)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

DEFAULT_MEDIA_TYPEback to summary
private static final MediaType DEFAULT_MEDIA_TYPE
encoderback to summary
private final Encoder<?> encoder
Annotations
@Nullable
loggerback to summary
private static final Log logger
WRITABLE_MEDIA_TYPESback to summary
private static final List<MediaType> WRITABLE_MEDIA_TYPES

Constructor Detail

ServerSentEventHttpMessageWriterback to summary
public ServerSentEventHttpMessageWriter()

Constructor without an Encoder. In this mode only String is supported for event data to be encoded.

ServerSentEventHttpMessageWriterback to summary
public ServerSentEventHttpMessageWriter(Encoder<?> encoder)

Constructor with JSON Encoder for encoding objects. Support for String event data is built-in.

Parameters
encoder:Encoder<?>

the Encoder to use (may be null)

Method Detail

canWriteback to summary
public boolean canWrite(ResolvableType elementType, MediaType mediaType)

Implements org.springframework.http.codec.HttpMessageWriter.canWrite.

Doc from org.springframework.http.codec.HttpMessageWriter.canWrite.

Whether the given object type is supported by this writer.

Parameters
elementType:ResolvableType

the type of object to check

mediaType:MediaType

the media type for the write (possibly null)

Returns:boolean

true if writable, false otherwise

Annotations
@Override
encodeback to summary
private Flux<Publisher<DataBuffer>> encode(Publisher<?> input, ResolvableType elementType, MediaType mediaType, DataBufferFactory factory, Map<String, Object> hints)
encodeEventback to summary
private <T> Flux<DataBuffer> encodeEvent(StringBuilder eventContent, T data, ResolvableType dataType, MediaType mediaType, DataBufferFactory factory, Map<String, Object> hints)
Annotations
@SuppressWarnings:unchecked
encodeTextback to summary
private DataBuffer encodeText(CharSequence text, MediaType mediaType, DataBufferFactory bufferFactory)
getEncodeHintsback to summary
private Map<String, Object> getEncodeHints(ResolvableType actualType, ResolvableType elementType, MediaType mediaType, ServerHttpRequest request, ServerHttpResponse response)
getEncoderback to summary
public Encoder<?> getEncoder()

Return the configured Encoder, if any.

Annotations
@Nullable
getWritableMediaTypesback to summary
public List<MediaType> getWritableMediaTypes()

Implements org.springframework.http.codec.HttpMessageWriter.getWritableMediaTypes.

Doc from org.springframework.http.codec.HttpMessageWriter.getWritableMediaTypes.

Return the list of media types supported by this Writer. The list may not apply to every possible target element type and calls to this method should typically be guarded via canWrite(elementType, null). The list may also exclude media types supported only for a specific element type. Alternatively, use getWritableMediaTypes(ResolvableType) for a more precise list.

Returns:List<MediaType>

the general list of supported media types

Annotations
@Override
writeback to summary
public Mono<Void> write(Publisher<?> input, ResolvableType elementType, MediaType mediaType, ReactiveHttpOutputMessage message, Map<String, Object> hints)

Implements org.springframework.http.codec.HttpMessageWriter.write.

Doc from org.springframework.http.codec.HttpMessageWriter.write.

Write a given stream of object to the output message.

Parameters
input:Publisher<?>

the objects to write

elementType:ResolvableType

the type of objects in the stream which must have been previously checked via canWrite(ResolvableType, MediaType)

mediaType:MediaType

the content type for the write (possibly null to indicate that the default content type of the writer must be used)

message:ReactiveHttpOutputMessage

the message to write to

hints:Map<String, Object>

additional information about how to encode and write

Returns:Mono<Void>

indicates completion or error

Annotations
@Override
writeback to summary
public Mono<Void> write(Publisher<?> input, ResolvableType actualType, ResolvableType elementType, MediaType mediaType, ServerHttpRequest request, ServerHttpResponse response, Map<String, Object> hints)

Overrides default org.springframework.http.codec.HttpMessageWriter.write.

Doc from org.springframework.http.codec.HttpMessageWriter.write.

Server-side only alternative to write(Publisher, ResolvableType, MediaType, ReactiveHttpOutputMessage, Map) with additional context available.

Parameters
actualType:ResolvableType

the actual return type of the method that returned the value; for annotated controllers, the MethodParameter can be accessed via ResolvableType#getSource().

elementType:ResolvableType

the type of Objects in the input stream

mediaType:MediaType

the content type to use (possibly null indicating the default content type of the writer should be used)

request:ServerHttpRequest

the current request

response:ServerHttpResponse

the current response

Returns:Mono<Void>

a Mono that indicates completion of writing or error

Annotations
@Override
writeFieldback to summary
private void writeField(String fieldName, Object fieldValue, StringBuilder sb)