Package org.springframework.http.codec

# Class EncoderHttpMessageWriter<T>

java.lang.Object
org.springframework.http.codec.EncoderHttpMessageWriter<T>

Type Parameters:
  `T` - the type of objects in the input stream

All Implemented Interfaces:
  `HttpMessageWriter<T>`

Direct Known Subclasses:
  `ProtobufHttpMessageWriter`

public class EncoderHttpMessageWriter<T> extends Object implements HttpMessageWriter<T>
`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.

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

## Constructor Summary

Constructors
Constructor
Description
`EncoderHttpMessageWriter(org.springframework.core.codec.Encoder<T> encoder)`
Create an instance wrapping the given `Encoder`.

## Method Summary

Modifier and Type
Method
Description
`boolean`
`canWrite(org.springframework.core.ResolvableType elementType, MediaType mediaType)`
Whether the given object type is supported by this writer.
`org.springframework.core.codec.Encoder<T>`
`getEncoder()`
Return the `Encoder` of this writer.
`List<MediaType>`
`getWritableMediaTypes()`
Return the list of media types supported by this Writer.
`List<MediaType>`
`getWritableMediaTypes(org.springframework.core.ResolvableType elementType)`
Return the list of media types supported by this Writer for the given type of element.
`protected Map<String,Object>`
`getWriteHints(org.springframework.core.ResolvableType streamType, org.springframework.core.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.
`reactor.core.publisher.Mono<Void>`
`write(Publisher<? extends T> inputStream, org.springframework.core.ResolvableType actualType, org.springframework.core.ResolvableType elementType, MediaType mediaType, ServerHttpRequest request, ServerHttpResponse response, Map<String,Object> hints)`
Server-side only alternative to `HttpMessageWriter.write(Publisher, ResolvableType, MediaType, ReactiveHttpOutputMessage, Map)` with additional context available.
`reactor.core.publisher.Mono<Void>`
`write(Publisher<? extends T> inputStream, org.springframework.core.ResolvableType elementType, MediaType mediaType, ReactiveHttpOutputMessage message, Map<String,Object> hints)`
Write a given stream of object to the output message.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### EncoderHttpMessageWriter

public EncoderHttpMessageWriter(org.springframework.core.codec.Encoder<T> encoder)
Create an instance wrapping the given `Encoder`.

## Method Details

### getEncoder

public org.springframework.core.codec.Encoder<T> getEncoder()
Return the `Encoder` of this writer.

### getWritableMediaTypes

public List<MediaType> getWritableMediaTypes()
Description copied from interface: `HttpMessageWriter`
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 `HttpMessageWriter.getWritableMediaTypes(ResolvableType)` for a more precise list.

Specified by:
  `getWritableMediaTypes` in interface `HttpMessageWriter<T>`
Returns:
  the general list of supported media types

### getWritableMediaTypes

public List<MediaType> getWritableMediaTypes(org.springframework.core.ResolvableType elementType)
Description copied from interface: `HttpMessageWriter`
Return the list of media types supported by this Writer for the given type of element. This list may differ from `HttpMessageWriter.getWritableMediaTypes()` if the Writer doesn't support the element type, or if it supports it only for a subset of media types.

Specified by:
  `getWritableMediaTypes` in interface `HttpMessageWriter<T>`
Parameters:
  `elementType` - the type of element to encode
Returns:
  the list of media types supported for the given class

### canWrite

public boolean canWrite(org.springframework.core.ResolvableType elementType, @Nullable MediaType mediaType)
Description copied from interface: `HttpMessageWriter`
Whether the given object type is supported by this writer.

Specified by:
  `canWrite` in interface `HttpMessageWriter<T>`
Parameters:
  `elementType` - the type of object to check
  `mediaType` - the media type for the write (possibly `null`)
Returns:
  `true` if writable, `false` otherwise

### write

public reactor.core.publisher.Mono<Void> write(Publisher<? extends T> inputStream, org.springframework.core.ResolvableType elementType, @Nullable MediaType mediaType, ReactiveHttpOutputMessage message, Map<String,Object> hints)
Description copied from interface: `HttpMessageWriter`
Write a given stream of object to the output message.

Specified by:
  `write` in interface `HttpMessageWriter<T>`
Parameters:
  `inputStream` - the objects to write
  `elementType` - the type of objects in the stream which must have been previously checked via `HttpMessageWriter.canWrite(ResolvableType, MediaType)`
  `mediaType` - the content type for the write (possibly `null` to indicate that the default content type of the writer must be used)
  `message` - the message to write to
  `hints` - additional information about how to encode and write
Returns:
  indicates completion or error

### write

public reactor.core.publisher.Mono<Void> write(Publisher<? extends T> inputStream, org.springframework.core.ResolvableType actualType, org.springframework.core.ResolvableType elementType, @Nullable MediaType mediaType, ServerHttpRequest request, ServerHttpResponse response, Map<String,Object> hints)
Description copied from interface: `HttpMessageWriter`
Server-side only alternative to `HttpMessageWriter.write(Publisher, ResolvableType, MediaType, ReactiveHttpOutputMessage, Map)` with additional context available.

Specified by:
  `write` in interface `HttpMessageWriter<T>`
  `actualType` - the actual return type of the method that returned the value; for annotated controllers, the `MethodParameter` can be accessed via `ResolvableType.getSource()`.
  `elementType` - the type of Objects in the input stream
  `mediaType` - the content type to use (possibly `null` indicating the default content type of the writer should be used)
  `request` - the current request
  `response` - the current response
Returns:
  a `Mono` that indicates completion of writing or error

### getWriteHints

protected Map<String,Object> getWriteHints(org.springframework.core.ResolvableType streamType, org.springframework.core.ResolvableType elementType, @Nullable 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`.