public abstract class AbstractHttpMessageConverter<T> extends Object implements HttpMessageConverter<T>
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.
Modifier and Type | Field and Description |
---|---|
protected Log |
logger
Logger available to subclasses
|
Modifier | Constructor and Description |
---|---|
protected |
AbstractHttpMessageConverter()
Construct an
AbstractHttpMessageConverter with no 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. |
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)
|
boolean |
canWrite(Class<?> clazz,
MediaType mediaType)
|
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.
|
protected MediaType |
getDefaultContentType(T t)
Returns the default content type for the given type.
|
List<MediaType> |
getSupportedMediaTypes()
Return the list of
MediaType objects 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 |
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.
|
protected final Log logger
protected AbstractHttpMessageConverter()
AbstractHttpMessageConverter
with no supported media types.protected AbstractHttpMessageConverter(MediaType supportedMediaType)
AbstractHttpMessageConverter
with one supported media type.supportedMediaType
- the supported media typeprotected AbstractHttpMessageConverter(MediaType... supportedMediaTypes)
AbstractHttpMessageConverter
with multiple supported media types.supportedMediaTypes
- the supported media typespublic void setSupportedMediaTypes(List<MediaType> supportedMediaTypes)
MediaType
objects supported by this converter.public List<MediaType> getSupportedMediaTypes()
HttpMessageConverter
MediaType
objects supported by this converter.getSupportedMediaTypes
in interface HttpMessageConverter<T>
public boolean canRead(Class<?> clazz, MediaType mediaType)
canRead
in interface HttpMessageConverter<T>
clazz
- the class to test for readabilitymediaType
- the media type to read, can be null
if not specified.
Typically the value of a Content-Type
header.true
if readable; false
otherwiseprotected boolean canRead(MediaType mediaType)
mediaType
- the media type to read, can be null
if not specified.
Typically the value of a Content-Type
header.true
if the supported media types include the media type,
or if the media type is null
public boolean canWrite(Class<?> clazz, MediaType mediaType)
canWrite
in interface HttpMessageConverter<T>
clazz
- the class to test for writabilitymediaType
- the media type to write, can be null
if not specified.
Typically the value of an Accept
header.true
if writable; false
otherwiseprotected boolean canWrite(MediaType mediaType)
true
if the given media type includes any of the
supported media types.mediaType
- the media type to write, can be null
if not specified.
Typically the value of an Accept
header.true
if the supported media types are compatible with the media type,
or if the media type is null
public final T read(Class<? extends T> clazz, HttpInputMessage inputMessage) throws IOException
readInternal(Class, HttpInputMessage)
.
Future implementations might add some default behavior, however.read
in interface HttpMessageConverter<T>
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 fromIOException
- in case of I/O errorspublic final void write(T t, MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException
addDefaultHeaders(org.springframework.http.HttpHeaders, T, org.springframework.http.MediaType)
,
and then calls writeInternal(T, org.springframework.http.HttpOutputMessage)
.write
in interface HttpMessageConverter<T>
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 toIOException
- in case of I/O errorsHttpMessageNotWritableException
- in case of conversion errorsprotected void addDefaultHeaders(HttpHeaders headers, T t, MediaType contentType) throws IOException
This implementation delegates to getDefaultContentType(Object)
if a content
type was not provided, calls getContentLength(T, org.springframework.http.MediaType)
, and sets the corresponding headers.
IOException
protected MediaType getDefaultContentType(T t) throws IOException
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.
t
- the type to return the content type fornull
if not knownIOException
protected Long getContentLength(T t, MediaType contentType) throws IOException
By default, this returns null
, meaning that the content length is unknown.
Can be overridden in subclasses.
t
- the type to return the content length fornull
if not knownIOException
protected abstract boolean supports(Class<?> clazz)
clazz
- the class to test for supporttrue
if supported; false
otherwiseprotected abstract T readInternal(Class<? extends T> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException
read(java.lang.Class<? extends T>, org.springframework.http.HttpInputMessage)
.clazz
- the type of object to returninputMessage
- the HTTP input message to read fromIOException
- in case of I/O errorsHttpMessageNotReadableException
- in case of conversion errorsprotected abstract void writeInternal(T t, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException
write(T, org.springframework.http.MediaType, org.springframework.http.HttpOutputMessage)
.t
- the object to write to the output messageoutputMessage
- the HTTP output message to write toIOException
- in case of I/O errorsHttpMessageNotWritableException
- in case of conversion errors