Package io.micronaut.http.body
Interface MessageBodyWriter<T>
- Type Parameters:
T- The generic type.
- All Known Subinterfaces:
MessageBodyHandler<T>,RawMessageBodyHandler<T>
- All Known Implementing Classes:
CharSequenceBodyWriter,DynamicMessageBodyWriter,TextPlainHandler,WritableBodyWriter
An interface that allows writing a message body for the client or the server.
Implementors can define beans that use Produces to restrict the applicable content types.
Use Order to specify the precedence of the writer with lower order corresponding to higher precedence.
- Since:
- 4.0.0
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptiondefault MessageBodyWriter<T>createSpecific(@NonNull io.micronaut.core.type.Argument<T> type) Prepare aMessageBodyWriterthat will write the given type.static @NonNull CharsetgetCharset(@NonNull io.micronaut.core.type.Headers headers) Resolve the charset.default booleantrueiff this closure can do a blocking read on the object it receives.default booleanisWriteable(@NonNull io.micronaut.core.type.Argument<T> type, @Nullable MediaType mediaType) Is the type writeable.default @NonNull io.micronaut.core.io.buffer.ByteBuffer<?>writeTo(@NonNull io.micronaut.core.type.Argument<T> type, @NonNull MediaType mediaType, T object, @NonNull io.micronaut.core.type.MutableHeaders outgoingHeaders, @NonNull io.micronaut.core.io.buffer.ByteBufferFactory<?, ?> bufferFactory) Writes an object to the given stream.voidwriteTo(@NonNull io.micronaut.core.type.Argument<T> type, @NonNull MediaType mediaType, T object, @NonNull io.micronaut.core.type.MutableHeaders outgoingHeaders, @NonNull OutputStream outputStream) Writes an object to the given output stream.
-
Method Details
-
isWriteable
default boolean isWriteable(@NonNull @NonNull io.micronaut.core.type.Argument<T> type, @Nullable @Nullable MediaType mediaType) Is the type writeable.- Parameters:
type- The typemediaType- The media type, can benull- Returns:
- True if is writable
-
createSpecific
default MessageBodyWriter<T> createSpecific(@NonNull @NonNull io.micronaut.core.type.Argument<T> type) Prepare aMessageBodyWriterthat will write the given type. This can be used for precomputing some route data.- Parameters:
type- The type- Returns:
- The closure
-
isBlocking
default boolean isBlocking()trueiff this closure can do a blocking read on the object it receives. For example, if this closure writes from anInputStream, that operation may be blocking and this method returnstrue.
Note that even when this isfalse,writeTo(Argument, MediaType, Object, MutableHeaders, OutputStream)may still block because theOutputStreamthat is passed as the write destination may still block.- Returns:
- Whether this writer may block
-
getCharset
@NonNull static @NonNull Charset getCharset(@NonNull @NonNull io.micronaut.core.type.Headers headers) Resolve the charset.- Parameters:
headers- The headers- Returns:
- The charset
-
writeTo
void writeTo(@NonNull @NonNull io.micronaut.core.type.Argument<T> type, @NonNull @NonNull MediaType mediaType, T object, @NonNull @NonNull io.micronaut.core.type.MutableHeaders outgoingHeaders, @NonNull @NonNull OutputStream outputStream) throws CodecException Writes an object to the given output stream.- Parameters:
type- The typemediaType- The media typeobject- The object to writeoutgoingHeaders- The HTTP headersoutputStream- The output stream- Throws:
CodecException- If an error occurs decoding
-
writeTo
@NonNull default @NonNull io.micronaut.core.io.buffer.ByteBuffer<?> writeTo(@NonNull @NonNull io.micronaut.core.type.Argument<T> type, @NonNull @NonNull MediaType mediaType, T object, @NonNull @NonNull io.micronaut.core.type.MutableHeaders outgoingHeaders, @NonNull @NonNull io.micronaut.core.io.buffer.ByteBufferFactory<?, ?> bufferFactory) throws CodecExceptionWrites an object to the given stream.- Parameters:
type- The typemediaType- The media typeobject- The object to writeoutgoingHeaders- The HTTP headersbufferFactory- A byte buffer factory- Returns:
- The encoded byte buffer
- Throws:
CodecException- If an error occurs decoding
-