T
- the converted object typepublic abstract class AbstractHttpMessageConverter<T> extends java.lang.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(java.nio.charset.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. |
Modifier and Type | Method and Description |
---|---|
protected void |
addDefaultHeaders(HttpHeaders headers,
T t,
MediaType contentType)
Add default headers to the output message.
|
boolean |
canRead(java.lang.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(java.lang.Class<?> clazz,
MediaType mediaType)
|
protected boolean |
canWrite(MediaType mediaType)
Returns
true if the given media type includes any of the
supported media types. |
protected java.lang.Long |
getContentLength(T t,
MediaType contentType)
Returns the content length for the given type.
|
java.nio.charset.Charset |
getDefaultCharset()
Return the default character set, if any.
|
protected MediaType |
getDefaultContentType(T t)
Returns the default content type for the given type.
|
java.util.List<MediaType> |
getSupportedMediaTypes()
Return the list of
MediaType objects supported by this converter. |
T |
read(java.lang.Class<? extends T> clazz,
HttpInputMessage inputMessage)
This implementation simple delegates to
readInternal(Class, HttpInputMessage) . |
protected abstract T |
readInternal(java.lang.Class<? extends T> clazz,
HttpInputMessage inputMessage)
Abstract template method that reads the actual object.
|
void |
setDefaultCharset(java.nio.charset.Charset defaultCharset)
Set the default character set, if any.
|
void |
setSupportedMediaTypes(java.util.List<MediaType> supportedMediaTypes)
Set the list of
MediaType objects supported by this converter. |
protected abstract boolean |
supports(java.lang.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 typesprotected AbstractHttpMessageConverter(java.nio.charset.Charset defaultCharset, MediaType... supportedMediaTypes)
AbstractHttpMessageConverter
with a default charset and
multiple supported media types.defaultCharset
- the default character setsupportedMediaTypes
- the supported media typespublic void setSupportedMediaTypes(java.util.List<MediaType> supportedMediaTypes)
MediaType
objects supported by this converter.public java.util.List<MediaType> getSupportedMediaTypes()
HttpMessageConverter
MediaType
objects supported by this converter.getSupportedMediaTypes
in interface HttpMessageConverter<T>
public void setDefaultCharset(@Nullable java.nio.charset.Charset defaultCharset)
@Nullable public java.nio.charset.Charset getDefaultCharset()
public boolean canRead(java.lang.Class<?> clazz, @Nullable 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(@Nullable 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(java.lang.Class<?> clazz, @Nullable 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(@Nullable 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(java.lang.Class<? extends T> clazz, HttpInputMessage inputMessage) throws java.io.IOException, HttpMessageNotReadableException
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 fromjava.io.IOException
- in case of I/O errorsHttpMessageNotReadableException
- in case of conversion errorspublic final void write(T t, @Nullable MediaType contentType, HttpOutputMessage outputMessage) throws java.io.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 tojava.io.IOException
- in case of I/O errorsHttpMessageNotWritableException
- in case of conversion errorsprotected void addDefaultHeaders(HttpHeaders headers, T t, @Nullable MediaType contentType) throws java.io.IOException
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.
java.io.IOException
@Nullable protected MediaType getDefaultContentType(T t) throws java.io.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 knownjava.io.IOException
@Nullable protected java.lang.Long getContentLength(T t, @Nullable MediaType contentType) throws java.io.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 knownjava.io.IOException
protected abstract boolean supports(java.lang.Class<?> clazz)
clazz
- the class to test for supporttrue
if supported; false
otherwiseprotected abstract T readInternal(java.lang.Class<? extends T> clazz, HttpInputMessage inputMessage) throws java.io.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 fromjava.io.IOException
- in case of I/O errorsHttpMessageNotReadableException
- in case of conversion errorsprotected abstract void writeInternal(T t, HttpOutputMessage outputMessage) throws java.io.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 tojava.io.IOException
- in case of I/O errorsHttpMessageNotWritableException
- in case of conversion errors