org.springframework.http.converter

Class AbstractHttpMessageConverter<T>

  • java.lang.Object
    • org.springframework.http.converter.AbstractHttpMessageConverter<T>
    • Field Detail

      • logger

        protected final Log logger
        Logger available to subclasses.
    • Constructor Detail

      • 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(java.nio.charset.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(java.util.List<MediaType> supportedMediaTypes)
        Set the list of MediaType objects supported by this converter.
      • setDefaultCharset

        public void setDefaultCharset(@Nullable
                                      java.nio.charset.Charset defaultCharset)
        Set the default character set, if any.
        Since:
        4.3
      • getDefaultCharset

        @Nullable
        public java.nio.charset.Charset getDefaultCharset()
        Return the default character set, if any.
        Since:
        4.3
      • canRead

        public boolean canRead(java.lang.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(java.lang.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
      • getContentLength

        @Nullable
        protected java.lang.Long getContentLength(T t,
                                                            @Nullable
                                                            MediaType contentType)
                                                     throws java.io.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:
        java.io.IOException
      • supports

        protected abstract boolean supports(java.lang.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