org.springframework.http.converter

## Class AbstractHttpMessageConverter<T>

- java.lang.Object
- - org.springframework.http.converter.AbstractHttpMessageConverter<T>

- Type Parameters:
    `T` - the converted object type
  All Implemented Interfaces:
    HttpMessageConverter<T>
  Direct Known Subclasses:
    AbstractGenericHttpMessageConverter, AbstractWireFeedHttpMessageConverter, AbstractXmlHttpMessageConverter, ByteArrayHttpMessageConverter, ObjectToStringHttpMessageConverter, ProtobufHttpMessageConverter, ResourceHttpMessageConverter, SourceHttpMessageConverter, StringHttpMessageConverter

      public abstract class AbstractHttpMessageConverter<T>
      extends Object
      implements HttpMessageConverter<T>

  Abstract base class for most `HttpMessageConverter` implementations.

  This base class adds support for setting supported `MediaTypes`, through the `supportedMediaTypes` bean property. It also adds support for `Content-Type` and `Content-Length` when writing to output messages.

  Since:
    3.0
  Author:
    Arjen Poutsma, Juergen Hoeller, Sebastien Deleuze

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected Log` | `logger` Logger available to subclasses.

  - ### Constructor Summary

    Constructors
    Modifier | Constructor and Description
    --- | ---
    `protected` | `AbstractHttpMessageConverter()` Construct an `AbstractHttpMessageConverter` with no supported media types.
    `protected` | `AbstractHttpMessageConverter(Charset defaultCharset, MediaType... supportedMediaTypes)` Construct an `AbstractHttpMessageConverter` with a default charset and multiple supported media types.
    `protected` | `AbstractHttpMessageConverter(MediaType... supportedMediaTypes)` Construct an `AbstractHttpMessageConverter` with multiple supported media types.
    `protected` | `AbstractHttpMessageConverter(MediaType supportedMediaType)` Construct an `AbstractHttpMessageConverter` with one supported media type.

  - ### Method Summary

    All Methods Instance Methods Abstract Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected void` | `addDefaultHeaders(HttpHeaders headers, T t, MediaType contentType)` Add default headers to the output message.
    `boolean` | `canRead(Class<?> clazz, MediaType mediaType)` This implementation checks if the given class is supported, and if the supported media types include the given media type.
    `protected boolean` | `canRead(MediaType mediaType)` Returns `true` if any of the supported media types `include` the given media type.
    `boolean` | `canWrite(Class<?> clazz, MediaType mediaType)` This implementation checks if the given class is supported, and if the supported media types include the given media type.
    `protected boolean` | `canWrite(MediaType mediaType)` Returns `true` if the given media type includes any of the supported media types.
    `protected Long` | `getContentLength(T t, MediaType contentType)` Returns the content length for the given type.
    `Charset` | `getDefaultCharset()` Return the default character set, if any.
    `protected MediaType` | `getDefaultContentType(T t)` Returns the default content type for the given type.
    `List<MediaType>` | `getSupportedMediaTypes()` Return the list of media types supported by this converter.
    `T` | `read(Class<? extends T> clazz, HttpInputMessage inputMessage)` This implementation simple delegates to `readInternal(Class, HttpInputMessage)`.
    `protected abstract T` | `readInternal(Class<? extends T> clazz, HttpInputMessage inputMessage)` Abstract template method that reads the actual object.
    `void` | `setDefaultCharset(Charset defaultCharset)` Set the default character set, if any.
    `void` | `setSupportedMediaTypes(List<MediaType> supportedMediaTypes)` Set the list of `MediaType` objects supported by this converter.
    `protected abstract boolean` | `supports(Class<?> clazz)` Indicates whether the given class is supported by this converter.
    `void` | `write(T t, MediaType contentType, HttpOutputMessage outputMessage)` This implementation sets the default headers by calling `addDefaultHeaders(org.springframework.http.HttpHeaders, T, org.springframework.http.MediaType)`, and then calls `writeInternal(T, org.springframework.http.HttpOutputMessage)`.
    `protected abstract void` | `writeInternal(T t, HttpOutputMessage outputMessage)` Abstract template method that writes the actual body.

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

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

    - ### Methods inherited from interface org.springframework.http.converter.HttpMessageConverter

      `getSupportedMediaTypes`

- - ### Field Detail

    - #### logger

          protected final Log logger

      Logger available to subclasses.

  - ### Constructor Detail

    - #### AbstractHttpMessageConverter

          protected AbstractHttpMessageConverter()

      Construct an `AbstractHttpMessageConverter` with no supported media types.
      See Also:
        `setSupportedMediaTypes(java.util.List<org.springframework.http.MediaType>)`

    - #### AbstractHttpMessageConverter

          protected AbstractHttpMessageConverter(MediaType supportedMediaType)

      Construct an `AbstractHttpMessageConverter` with one supported media type.
      Parameters:
        `supportedMediaType` - the supported media type

    - #### AbstractHttpMessageConverter

          protected AbstractHttpMessageConverter(MediaType... supportedMediaTypes)

      Construct an `AbstractHttpMessageConverter` with multiple supported media types.
      Parameters:
        `supportedMediaTypes` - the supported media types

    - #### AbstractHttpMessageConverter

          protected AbstractHttpMessageConverter(Charset defaultCharset,
                                                 MediaType... supportedMediaTypes)

      Construct an `AbstractHttpMessageConverter` with a default charset and multiple supported media types.
      Parameters:
        `defaultCharset` - the default character set
        `supportedMediaTypes` - the supported media types
      Since:
        4.3

  - ### Method Detail

    - #### setSupportedMediaTypes

          public void setSupportedMediaTypes(List<MediaType> supportedMediaTypes)

      Set the list of `MediaType` objects supported by this converter.

    - #### getSupportedMediaTypes

          public List<MediaType> getSupportedMediaTypes()

      Description copied from interface: `HttpMessageConverter`
      Return the list of media types supported by this converter. The list may not apply to every possible target element type and calls to this method should typically be guarded via `canWrite(clazz, null`. The list may also exclude MIME types supported only for a specific class. Alternatively, use `HttpMessageConverter.getSupportedMediaTypes(Class)` for a more precise list.
      Specified by:
        `getSupportedMediaTypes` in interface `HttpMessageConverter<T>`
      Returns:
        the list of supported media types

    - #### setDefaultCharset

          public void setDefaultCharset(@Nullable
                                        Charset defaultCharset)

      Set the default character set, if any.
      Since:
        4.3

    - #### getDefaultCharset

          @Nullable
          public Charset getDefaultCharset()

      Return the default character set, if any.
      Since:
        4.3

    - #### canRead

          public boolean canRead(Class<?> clazz,
                                 @Nullable
                                 MediaType mediaType)

      This implementation checks if the given class is supported, and if the supported media types include the given media type.
      Specified by:
        `canRead` in interface `HttpMessageConverter<T>`
      Parameters:
        `clazz` - the class to test for readability
        `mediaType` - the media type to read (can be `null` if not specified); typically the value of a `Content-Type` header.
      Returns:
        `true` if readable; `false` otherwise

    - #### canRead

          protected boolean canRead(@Nullable
                                    MediaType mediaType)

      Returns `true` if any of the supported media types `include` the given media type.
      Parameters:
        `mediaType` - the media type to read, can be `null` if not specified. Typically the value of a `Content-Type` header.
      Returns:
        `true` if the supported media types include the media type, or if the media type is `null`

    - #### canWrite

          public boolean canWrite(Class<?> clazz,
                                  @Nullable
                                  MediaType mediaType)

      This implementation checks if the given class is supported, and if the supported media types include the given media type.
      Specified by:
        `canWrite` in interface `HttpMessageConverter<T>`
      Parameters:
        `clazz` - the class to test for writability
        `mediaType` - the media type to write (can be `null` if not specified); typically the value of an `Accept` header.
      Returns:
        `true` if writable; `false` otherwise

    - #### canWrite

          protected boolean canWrite(@Nullable
                                     MediaType mediaType)

      Returns `true` if the given media type includes any of the supported media types.
      Parameters:
        `mediaType` - the media type to write, can be `null` if not specified. Typically the value of an `Accept` header.
      Returns:
        `true` if the supported media types are compatible with the media type, or if the media type is `null`

    - #### read

          public final T read(Class<? extends T> clazz,
                              HttpInputMessage inputMessage)
                       throws IOException,
                              HttpMessageNotReadableException

      This implementation simple delegates to `readInternal(Class, HttpInputMessage)`. Future implementations might add some default behavior, however.
      Specified by:
        `read` in interface `HttpMessageConverter<T>`
      Parameters:
        `clazz` - the type of object to return. This type must have previously been passed to the `canRead` method of this interface, which must have returned `true`.
        `inputMessage` - the HTTP input message to read from
      Returns:
        the converted object
      Throws:
        `IOException` - in case of I/O errors
        `HttpMessageNotReadableException` - in case of conversion errors

    - #### write

          public final void write(T t,
                                  @Nullable
                                  MediaType contentType,
                                  HttpOutputMessage outputMessage)
                           throws IOException,
                                  HttpMessageNotWritableException

      This implementation sets the default headers by calling `addDefaultHeaders(org.springframework.http.HttpHeaders, T, org.springframework.http.MediaType)`, and then calls `writeInternal(T, org.springframework.http.HttpOutputMessage)`.
      Specified by:
        `write` in interface `HttpMessageConverter<T>`
      Parameters:
        `t` - the object to write to the output message. The type of this object must have previously been passed to the `canWrite` method of this interface, which must have returned `true`.
        `contentType` - the content type to use when writing. May be `null` to indicate that the default content type of the converter must be used. If not `null`, this media type must have previously been passed to the `canWrite` method of this interface, which must have returned `true`.
        `outputMessage` - the message to write to
      Throws:
        `IOException` - in case of I/O errors
        `HttpMessageNotWritableException` - in case of conversion errors

    - #### addDefaultHeaders

          protected void addDefaultHeaders(HttpHeaders headers,
                                           T t,
                                           @Nullable
                                           MediaType contentType)
                                    throws IOException

      Add default headers to the output message.

      This implementation delegates to `getDefaultContentType(Object)` if a content type was not provided, set if necessary the default character set, calls `getContentLength(T, org.springframework.http.MediaType)`, and sets the corresponding headers.

      Throws:
        `IOException`
      Since:
        4.2

    - #### getDefaultContentType

          @Nullable
          protected MediaType getDefaultContentType(T t)
                                                       throws IOException

      Returns the default content type for the given type. Called when `write(T, org.springframework.http.MediaType, org.springframework.http.HttpOutputMessage)` is invoked without a specified content type parameter.

      By default, this returns the first element of the `supportedMediaTypes` property, if any. Can be overridden in subclasses.

      Parameters:
        `t` - the type to return the content type for
      Returns:
        the content type, or `null` if not known
      Throws:
        `IOException`

    - #### getContentLength

          @Nullable
          protected Long getContentLength(T t,
                                                    @Nullable
                                                    MediaType contentType)
                                             throws IOException

      Returns the content length for the given type.

      By default, this returns `null`, meaning that the content length is unknown. Can be overridden in subclasses.

      Parameters:
        `t` - the type to return the content length for
      Returns:
        the content length, or `null` if not known
      Throws:
        `IOException`

    - #### supports

          protected abstract boolean supports(Class<?> clazz)

      Indicates whether the given class is supported by this converter.
      Parameters:
        `clazz` - the class to test for support
      Returns:
        `true` if supported; `false` otherwise

    - #### readInternal

          protected abstract T readInternal(Class<? extends T> clazz,
                                            HttpInputMessage inputMessage)
                                     throws IOException,
                                            HttpMessageNotReadableException

      Abstract template method that reads the actual object. Invoked from `read(java.lang.Class<? extends T>, org.springframework.http.HttpInputMessage)`.
      Parameters:
        `clazz` - the type of object to return
        `inputMessage` - the HTTP input message to read from
      Returns:
        the converted object
      Throws:
        `IOException` - in case of I/O errors
        `HttpMessageNotReadableException` - in case of conversion errors

    - #### writeInternal

          protected abstract void writeInternal(T t,
                                                HttpOutputMessage outputMessage)
                                         throws IOException,
                                                HttpMessageNotWritableException

      Abstract template method that writes the actual body. Invoked from `write(T, org.springframework.http.MediaType, org.springframework.http.HttpOutputMessage)`.
      Parameters:
        `t` - the object to write to the output message
        `outputMessage` - the HTTP output message to write to
      Throws:
        `IOException` - in case of I/O errors
        `HttpMessageNotWritableException` - in case of conversion errors