Top Description Fields Constructors Methods
org.springframework.http.converter

public abstract Class AbstractHttpMessageConverter<T>

extends Object
implements HttpMessageConverter<T>
Class Inheritance
All Implemented Interfaces
org.springframework.http.converter.HttpMessageConverter
Known Direct Subclasses
org.springframework.http.converter.ByteArrayHttpMessageConverter, org.springframework.http.converter.ObjectToStringHttpMessageConverter, org.springframework.http.converter.ResourceHttpMessageConverter, org.springframework.http.converter.StringHttpMessageConverter, org.springframework.http.converter.feed.AbstractWireFeedHttpMessageConverter, org.springframework.http.converter.protobuf.ProtobufHttpMessageConverter, org.springframework.http.converter.xml.AbstractXmlHttpMessageConverter, org.springframework.http.converter.xml.SourceHttpMessageConverter, org.springframework.http.converter.AbstractGenericHttpMessageConverter
Type Parameters
<T>
the converted object type
Imports
java.io.IOException, .OutputStream, java.nio.charset.Charset, java.util.Arrays, .Collections, .List, org.apache.commons.logging.Log, org.springframework.http.HttpHeaders, .HttpInputMessage, .HttpLogging, .HttpOutputMessage, .MediaType, .StreamingHttpOutputMessage, org.springframework.lang.Nullable, org.springframework.util.Assert

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.

Authors
Arjen Poutsma, Juergen Hoeller, Sebastien Deleuze
Since
3.0

Field Summary

Modifier and TypeField and Description
private Charset
protected final Log
logger

Logger available to subclasses.

private List<MediaType>

Constructor Summary

AccessConstructor and Description
protected
AbstractHttpMessageConverter()

Construct an AbstractHttpMessageConverter with no supported media types.

protected
AbstractHttpMessageConverter(MediaType
the supported media type
supportedMediaType
)

Construct an AbstractHttpMessageConverter with one supported media type.

protected
AbstractHttpMessageConverter(MediaType...
the supported media types
supportedMediaTypes
)

Construct an AbstractHttpMessageConverter with multiple supported media types.

protected
AbstractHttpMessageConverter(Charset
the default character set
defaultCharset
,
MediaType...
the supported media types
supportedMediaTypes
)

Construct an AbstractHttpMessageConverter with a default charset and multiple supported media types.

Method Summary

Modifier and TypeMethod and Description
protected void
addDefaultHeaders(HttpHeaders headers, T t, MediaType contentType)

Add default headers to the output message.

public boolean
canRead(Class<?>
the class to test for readability
clazz
,
MediaType
the media type to read (can be null if not specified); typically the value of a Content-Type header.
mediaType
)

Implements org.springframework.http.converter.HttpMessageConverter.canRead.

This implementation checks if the given class is supported, and if the supported media types include the given media type.
protected boolean

Returns:

true if the supported media types include the media type, or if the media type is null
canRead
(MediaType
the media type to read, can be null if not specified. Typically the value of a Content-Type header.
mediaType
)

Returns true if any of the supported media types include the given media type.

public boolean
canWrite(Class<?>
the class to test for writability
clazz
,
MediaType
the media type to write (can be null if not specified); typically the value of an Accept header.
mediaType
)

Implements org.springframework.http.converter.HttpMessageConverter.canWrite.

This implementation checks if the given class is supported, and if the supported media types include the given media type.
protected boolean

Returns:

true if the supported media types are compatible with the media type, or if the media type is null
canWrite
(MediaType
the media type to write, can be null if not specified. Typically the value of an Accept header.
mediaType
)

Returns true if the given media type includes any of the supported media types.

protected Long

Returns:

the content length, or null if not known
getContentLength
(T
the type to return the content length for
t
,
MediaType contentType)

Returns the content length for the given type.

public Charset
getDefaultCharset()

Return the default character set, if any.

protected MediaType

Returns:

the content type, or null if not known
getDefaultContentType
(T
the type to return the content type for
t
)

Returns the default content type for the given type.

public List<MediaType>
getSupportedMediaTypes()

Implements org.springframework.http.converter.HttpMessageConverter.getSupportedMediaTypes.

Return the list of media types supported by this converter.
public final T
read(Class<? extends T>
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.
clazz
,
HttpInputMessage
the HTTP input message to read from
inputMessage
)

Implements org.springframework.http.converter.HttpMessageConverter.read.

This implementation simple delegates to readInternal(Class, HttpInputMessage).
protected abstract T

Returns:

the converted object
readInternal
(Class<? extends T>
the type of object to return
clazz
,
HttpInputMessage
the HTTP input message to read from
inputMessage
)

Abstract template method that reads the actual object.

public void
setDefaultCharset(Charset defaultCharset)

Set the default character set, if any.

public void
setSupportedMediaTypes(List<MediaType> supportedMediaTypes)

Set the list of MediaType objects supported by this converter.

protected abstract boolean

Returns:

true if supported; false otherwise
supports
(Class<?>
the class to test for support
clazz
)

Indicates whether the given class is supported by this converter.

protected boolean

Returns:

true if t can be written repeatedly; false otherwise
supportsRepeatableWrites
(T
the object t
t
)

Indicates whether this message converter can write the given object multiple times.

public final void
write(final 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.
t
,
MediaType
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.
contentType
,
HttpOutputMessage
the message to write to
outputMessage
)

Implements org.springframework.http.converter.HttpMessageConverter.write.

This implementation sets the default headers by calling addDefaultHeaders, and then calls writeInternal.
protected abstract void
writeInternal(T
the object to write to the output message
t
,
HttpOutputMessage
the HTTP output message to write to
outputMessage
)

Abstract template method that writes the actual body.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

defaultCharsetback to summary
private Charset defaultCharset
Annotations
@Nullable
loggerback to summary
protected final Log logger

Logger available to subclasses.

supportedMediaTypesback to summary
private List<MediaType> supportedMediaTypes

Constructor Detail

AbstractHttpMessageConverterback to summary
protected AbstractHttpMessageConverter()

Construct an AbstractHttpMessageConverter with no supported media types.

See Also
setSupportedMediaTypes
AbstractHttpMessageConverterback to summary
protected AbstractHttpMessageConverter(MediaType supportedMediaType)

Construct an AbstractHttpMessageConverter with one supported media type.

Parameters
supportedMediaType:MediaType

the supported media type

AbstractHttpMessageConverterback to summary
protected AbstractHttpMessageConverter(MediaType... supportedMediaTypes)

Construct an AbstractHttpMessageConverter with multiple supported media types.

Parameters
supportedMediaTypes:MediaType[]

the supported media types

AbstractHttpMessageConverterback to summary
protected AbstractHttpMessageConverter(Charset defaultCharset, MediaType... supportedMediaTypes)

Construct an AbstractHttpMessageConverter with a default charset and multiple supported media types.

Parameters
defaultCharset:Charset

the default character set

supportedMediaTypes:MediaType[]

the supported media types

Since
4.3

Method Detail

addDefaultHeadersback to summary
protected void addDefaultHeaders(HttpHeaders headers, T t, 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, and sets the corresponding headers.

Since
4.2
canReadback to summary
public boolean canRead(Class<?> clazz, MediaType mediaType)

Implements org.springframework.http.converter.HttpMessageConverter.canRead.

This implementation checks if the given class is supported, and if the supported media types include the given media type.

Parameters
clazz:Class<?>

Doc from org.springframework.http.converter.HttpMessageConverter.canRead.

the class to test for readability

mediaType:MediaType

Doc from org.springframework.http.converter.HttpMessageConverter.canRead.

the media type to read (can be null if not specified); typically the value of a Content-Type header.

Returns:boolean

Doc from org.springframework.http.converter.HttpMessageConverter.canRead.

true if readable; false otherwise

Annotations
@Override
canReadback to summary
protected boolean canRead(MediaType mediaType)

Returns true if any of the supported media types include the given media type.

Parameters
mediaType:MediaType

the media type to read, can be null if not specified. Typically the value of a Content-Type header.

Returns:boolean

true if the supported media types include the media type, or if the media type is null

canWriteback to summary
public boolean canWrite(Class<?> clazz, MediaType mediaType)

Implements org.springframework.http.converter.HttpMessageConverter.canWrite.

This implementation checks if the given class is supported, and if the supported media types include the given media type.

Parameters
clazz:Class<?>

Doc from org.springframework.http.converter.HttpMessageConverter.canWrite.

the class to test for writability

mediaType:MediaType

Doc from org.springframework.http.converter.HttpMessageConverter.canWrite.

the media type to write (can be null if not specified); typically the value of an Accept header.

Returns:boolean

Doc from org.springframework.http.converter.HttpMessageConverter.canWrite.

true if writable; false otherwise

Annotations
@Override
canWriteback to summary
protected boolean canWrite(MediaType mediaType)

Returns true if the given media type includes any of the supported media types.

Parameters
mediaType:MediaType

the media type to write, can be null if not specified. Typically the value of an Accept header.

Returns:boolean

true if the supported media types are compatible with the media type, or if the media type is null

getContentLengthback to summary
protected Long getContentLength(T t, 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:T

the type to return the content length for

Returns:Long

the content length, or null if not known

Annotations
@Nullable
getDefaultCharsetback to summary
public Charset getDefaultCharset()

Return the default character set, if any.

Annotations
@Nullable
Since
4.3
getDefaultContentTypeback to summary
protected MediaType getDefaultContentType(T t) throws IOException

Returns the default content type for the given type. Called when write 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:T

the type to return the content type for

Returns:MediaType

the content type, or null if not known

Annotations
@Nullable
getSupportedMediaTypesback to summary
public List<MediaType> getSupportedMediaTypes()

Implements org.springframework.http.converter.HttpMessageConverter.getSupportedMediaTypes.

Doc from org.springframework.http.converter.HttpMessageConverter.getSupportedMediaTypes.

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 getSupportedMediaTypes(Class) for a more precise list.

Returns:List<MediaType>

the list of supported media types

Annotations
@Override
readback to summary
public final T read(Class<? extends T> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException

Implements org.springframework.http.converter.HttpMessageConverter.read.

This implementation simple delegates to readInternal(Class, HttpInputMessage). Future implementations might add some default behavior, however.

Parameters
clazz:Class<? extends T>

Doc from org.springframework.http.converter.HttpMessageConverter.read.

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:HttpInputMessage

Doc from org.springframework.http.converter.HttpMessageConverter.read.

the HTTP input message to read from

Returns:T

Doc from org.springframework.http.converter.HttpMessageConverter.read.

the converted object

Annotations
@Override
Exceptions
IOException:

Doc from org.springframework.http.converter.HttpMessageConverter.read.

in case of I/O errors

HttpMessageNotReadableException:

Doc from org.springframework.http.converter.HttpMessageConverter.read.

in case of conversion errors

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

Abstract template method that reads the actual object. Invoked from read.

Parameters
clazz:Class<? extends T>

the type of object to return

inputMessage:HttpInputMessage

the HTTP input message to read from

Returns:T

the converted object

Exceptions
IOException:
in case of I/O errors
HttpMessageNotReadableException:
in case of conversion errors
setDefaultCharsetback to summary
public void setDefaultCharset(Charset defaultCharset)

Set the default character set, if any.

Since
4.3
setSupportedMediaTypesback to summary
public void setSupportedMediaTypes(List<MediaType> supportedMediaTypes)

Set the list of MediaType objects supported by this converter.

supportsback to summary
protected abstract boolean supports(Class<?> clazz)

Indicates whether the given class is supported by this converter.

Parameters
clazz:Class<?>

the class to test for support

Returns:boolean

true if supported; false otherwise

supportsRepeatableWritesback to summary
protected boolean supportsRepeatableWrites(T t)

Indicates whether this message converter can write the given object multiple times.

Default implementation returns false.

Parameters
t:T

the object t

Returns:boolean

true if t can be written repeatedly; false otherwise

Since
6.1
writeback to summary
public final void write(final T t, MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException

Implements org.springframework.http.converter.HttpMessageConverter.write.

This implementation sets the default headers by calling addDefaultHeaders, and then calls writeInternal.

Parameters
t:T

Doc from org.springframework.http.converter.HttpMessageConverter.write.

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:MediaType

Doc from org.springframework.http.converter.HttpMessageConverter.write.

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:HttpOutputMessage

Doc from org.springframework.http.converter.HttpMessageConverter.write.

the message to write to

Annotations
@Override
Exceptions
IOException:

Doc from org.springframework.http.converter.HttpMessageConverter.write.

in case of I/O errors

HttpMessageNotWritableException:

Doc from org.springframework.http.converter.HttpMessageConverter.write.

in case of conversion errors

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

Abstract template method that writes the actual body. Invoked from write.

Parameters
t:T

the object to write to the output message

outputMessage:HttpOutputMessage

the HTTP output message to write to

Exceptions
IOException:
in case of I/O errors
HttpMessageNotWritableException:
in case of conversion errors