|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.springframework.http.converter.AbstractHttpMessageConverter<T>
public abstract class AbstractHttpMessageConverter<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.
Field Summary | |
---|---|
protected Log |
logger
Logger available to subclasses |
Constructor Summary | |
---|---|
protected |
AbstractHttpMessageConverter()
Construct an AbstractHttpMessageConverter with no supported media types. |
protected |
AbstractHttpMessageConverter(MediaType... supportedMediaTypes)
Construct an AbstractHttpMessageConverter with multiple supported media type. |
protected |
AbstractHttpMessageConverter(MediaType supportedMediaType)
Construct an AbstractHttpMessageConverter with one supported media type. |
Method Summary | |
---|---|
boolean |
canRead(Class<? extends T> clazz,
MediaType mediaType)
Indicates whether the given class can be read by this converter. |
boolean |
canWrite(Class<? extends T> clazz,
MediaType mediaType)
Indicates whether the given class can be written by this converter. |
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. |
protected boolean |
isSupported(MediaType mediaType)
Returns true if any of the supported media types include the given media type. |
T |
read(Class<T> clazz,
HttpInputMessage inputMessage)
Read an object of the given type form the given input message, and returns it. |
protected abstract T |
readInternal(Class<T> clazz,
HttpInputMessage inputMessage)
Abstract template method that reads the actualy object. |
void |
setSupportedMediaTypes(List<MediaType> supportedMediaTypes)
Set the list of MediaType objects supported by this converter. |
protected abstract boolean |
supports(Class<? extends T> clazz)
Indicates whether the given class is supported by this converter. |
void |
write(T t,
MediaType contentType,
HttpOutputMessage outputMessage)
Write an given object to the given output message. |
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 |
Field Detail |
---|
protected final Log logger
Constructor Detail |
---|
protected AbstractHttpMessageConverter()
AbstractHttpMessageConverter
with no supported media types.
setSupportedMediaTypes(java.util.List)
protected AbstractHttpMessageConverter(MediaType supportedMediaType)
AbstractHttpMessageConverter
with one supported media type.
supportedMediaType
- the supported media typeprotected AbstractHttpMessageConverter(MediaType... supportedMediaTypes)
AbstractHttpMessageConverter
with multiple supported media type.
supportedMediaTypes
- the supported media typesMethod Detail |
---|
public 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<? extends T> clazz, MediaType mediaType)
This implementation checks if the given class is supported, and if the supported media types include the given media type.
canRead
in interface HttpMessageConverter<T>
clazz
- the class to test for readabilitymediaType
- the media type to read, can be null
if not specified
true
if readable; false
otherwisepublic boolean canWrite(Class<? extends T> clazz, MediaType mediaType)
This implementation checks if the given class is supported, and if the supported media types include the given media type.
canWrite
in interface HttpMessageConverter<T>
clazz
- the class to test for writabilitymediaType
- the media type to write, can be null
if not specified
true
if writable; false
otherwiseprotected boolean isSupported(MediaType mediaType)
mediaType
- the media type
null
protected abstract boolean supports(Class<? extends T> clazz)
clazz
- the class to test for support
true
if supported; false
otherwisepublic final T read(Class<T> clazz, HttpInputMessage inputMessage) throws IOException
This implementation simple delegates to 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 from
IOException
- in case of I/O errorsprotected abstract T readInternal(Class<T> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException
read(Class, HttpInputMessage)
.
clazz
- the type of object to returninputMessage
- the HTTP input message to read from
IOException
- in case of I/O errors
HttpMessageNotReadableException
- in case of conversion errorspublic final void write(T t, MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException
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 on the output message. It 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 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
IOException
- in case of I/O errors
HttpMessageNotWritableException
- in case of conversion errorsprotected MediaType getDefaultContentType(T t)
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 overriden in subclasses.
t
- the type to return the content type for
null
if not knownprotected Long getContentLength(T t, MediaType contentType)
By default, this returns null
, meaning that the content length is unknown. Can be overriden in
subclasses.
t
- the type to return the content length for
null
if not knownprotected 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 message to write to
IOException
- in case of I/O errors
HttpMessageNotWritableException
- in case of conversion errors
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |