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

public Class EncoderHttpMessageWriter<T>

extends Object
implements HttpMessageWriter<T>
Class Inheritance
All Implemented Interfaces
org.springframework.http.codec.HttpMessageWriter
Known Direct Subclasses
org.springframework.http.codec.protobuf.ProtobufHttpMessageWriter
Type Parameters
<T>
the type of objects in the input stream
Imports
java.util.List, .Map, org.apache.commons.logging.Log, org.reactivestreams.Publisher, reactor.core.publisher.Flux, .Mono, org.springframework.core.ResolvableType, org.springframework.core.codec.AbstractEncoder, .Encoder, .Hints, org.springframework.core.io.buffer.DataBuffer, .DataBufferUtils, org.springframework.http.HttpLogging, .MediaType, .ReactiveHttpOutputMessage, org.springframework.http.server.reactive.ServerHttpRequest, .ServerHttpResponse, org.springframework.lang.Nullable, org.springframework.util.Assert, .StringUtils

HttpMessageWriter that wraps and delegates to an Encoder.

Also a HttpMessageWriter that pre-resolves encoding hints from the extra information available on the server side such as the request or controller method annotations.

Authors
Arjen Poutsma, Sebastien Deleuze, Rossen Stoyanchev, Brian Clozel, Sam Brannen
Since
5.0

Field Summary

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

Constructor Summary

AccessConstructor and Description
public
EncoderHttpMessageWriter(Encoder<T> encoder)

Create an instance wrapping the given Encoder.

Method Summary

Modifier and TypeMethod and Description
private static MediaType
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.

public Encoder<T>
getEncoder()

Return the Encoder of this writer.

public List<MediaType>
getWritableMediaTypes()

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

Return the list of media types supported by this Writer.

public List<MediaType>
getWritableMediaTypes(ResolvableType
the type of element to encode
elementType
)

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

Return the list of media types supported by this Writer for the given type of element.

protected Map<String, Object>
getWriteHints(ResolvableType streamType, ResolvableType elementType, MediaType mediaType, ServerHttpRequest request, ServerHttpResponse response)

Get additional hints for encoding for example based on the server request or annotations from controller method parameters.

private static MediaType
private static void
initLogger(Encoder<?> encoder)

private boolean
private boolean
matchParameters(MediaType streamingMediaType, MediaType mediaType)

private MediaType
private static boolean
public Mono<Void>
write(Publisher<? extends T>
the objects to write
inputStream
,
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<? extends T> inputStream, 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.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

defaultMediaTypeback to summary
private final MediaType defaultMediaType
Annotations
@Nullable
encoderback to summary
private final Encoder<T> encoder
loggerback to summary
private static final Log logger
mediaTypesback to summary
private final List<MediaType> mediaTypes

Constructor Detail

EncoderHttpMessageWriterback to summary
public EncoderHttpMessageWriter(Encoder<T> encoder)

Create an instance wrapping the given Encoder.

Method Detail

addDefaultCharsetback to summary
private static MediaType addDefaultCharset(MediaType main, MediaType defaultType)
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
getEncoderback to summary
public Encoder<T> getEncoder()

Return the Encoder of this writer.

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
getWritableMediaTypesback to summary
public List<MediaType> getWritableMediaTypes(ResolvableType elementType)

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

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

Return the list of media types supported by this Writer for the given type of element. This list may differ from getWritableMediaTypes() if the Writer doesn't support the element type, or if it supports it only for a subset of media types.

Parameters
elementType:ResolvableType

the type of element to encode

Returns:List<MediaType>

the list of media types supported for the given class

Annotations
@Override
getWriteHintsback to summary
protected Map<String, Object> getWriteHints(ResolvableType streamType, ResolvableType elementType, MediaType mediaType, ServerHttpRequest request, ServerHttpResponse response)

Get additional hints for encoding for example based on the server request or annotations from controller method parameters. By default, delegate to the encoder if it is an instance of HttpMessageEncoder.

initDefaultMediaTypeback to summary
private static MediaType initDefaultMediaType(List<MediaType> mediaTypes)
Annotations
@Nullable
initLoggerback to summary
private static void initLogger(Encoder<?> encoder)
isStreamingMediaTypeback to summary
private boolean isStreamingMediaType(MediaType mediaType)
matchParametersback to summary
private boolean matchParameters(MediaType streamingMediaType, MediaType mediaType)
updateContentTypeback to summary
private MediaType updateContentType(ReactiveHttpOutputMessage message, MediaType mediaType)
Annotations
@Nullable
useFallbackback to summary
private static boolean useFallback(MediaType main, MediaType fallback)
writeback to summary
public Mono<Void> write(Publisher<? extends T> inputStream, 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
inputStream:Publisher<? extends T>

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<? extends T> inputStream, 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