Interface MessageBodyWriter<T>

Type Parameters:
T - The generic type.
All Known Subinterfaces:
MessageBodyHandler<T>, ResponseBodyWriter<T>, TypedMessageBodyHandler<T>, TypedMessageBodyWriter<T>
All Known Implementing Classes:
CharSequenceBodyWriter, FileBodyWriter, InputStreamBodyWriter, JsonMessageHandler, NettyByteBufMessageBodyHandler, NettyJsonHandler, NettyJsonStreamHandler, StreamFileBodyWriter, SystemFileBodyWriter, WritableBodyWriter

@Indexed(MessageBodyWriter.class) public interface MessageBodyWriter<T>
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 Details

    • isWriteable

      default boolean isWriteable(Argument<T> type, @Nullable MediaType mediaType)
      Is the type writeable.
      Parameters:
      type - The type
      mediaType - The media type, can be null
      Returns:
      True if is writable
    • createSpecific

      default MessageBodyWriter<T> createSpecific(Argument<T> type)
      Prepare a MessageBodyWriter that 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()
      true iff this closure can do a blocking read on the object it receives. For example, if this closure writes from an InputStream, that operation may be blocking and this method returns true.
      Note that even when this is false, writeTo(Argument, MediaType, Object, MutableHeaders, OutputStream) may still block because the OutputStream that is passed as the write destination may still block.
      Returns:
      Whether this writer may block
    • getCharset

      static Charset getCharset(Headers headers)
      Resolve the charset.
      Parameters:
      headers - The headers
      Returns:
      The charset
    • getCharset

      static Charset getCharset(@Nullable MediaType mediaType, Headers headers)
      Resolve the charset.
      Parameters:
      mediaType - The mediaType
      headers - The headers
      Returns:
      The charset
    • findCharset

      static Optional<Charset> findCharset(@Nullable MediaType mediaType, Headers headers)
      Resolve the charset.
      Parameters:
      mediaType - The mediaType
      headers - The headers
      Returns:
      The charset
    • findCharset

      static Optional<Charset> findCharset(Headers headers)
      Resolve the charset.
      Parameters:
      headers - The headers
      Returns:
      The charset
    • writeTo

      void writeTo(Argument<T> type, MediaType mediaType, T object, MutableHeaders outgoingHeaders, OutputStream outputStream) throws CodecException
      Writes an object to the given output stream.
      Parameters:
      type - The type
      mediaType - The media type
      object - The object to write
      outgoingHeaders - The HTTP headers
      outputStream - The output stream
      Throws:
      CodecException - If an error occurs decoding
    • writeTo

      default ByteBuffer<?> writeTo(Argument<T> type, MediaType mediaType, T object, MutableHeaders outgoingHeaders, ByteBufferFactory<?,?> bufferFactory) throws CodecException
      Writes an object to the given stream.
      Parameters:
      type - The type
      mediaType - The media type
      object - The object to write
      outgoingHeaders - The HTTP headers
      bufferFactory - A byte buffer factory
      Returns:
      The encoded byte buffer
      Throws:
      CodecException - If an error occurs decoding