Class AbstractHttpMessageConverter<T>
- java.lang.Object
- org.springframework.http.converter.AbstractHttpMessageConverter<T>
- All Implemented Interfaces:
HttpMessageConverter<T>
- Direct Known Subclasses:
AbstractGenericHttpMessageConverter,AbstractWireFeedHttpMessageConverter,AbstractXmlHttpMessageConverter,ByteArrayHttpMessageConverter,ObjectToStringHttpMessageConverter,ProtobufHttpMessageConverter,ResourceHttpMessageConverter,SourceHttpMessageConverter,StringHttpMessageConverter
public abstract class AbstractHttpMessageConverter<T> extends Object implements HttpMessageConverter<T>
Abstract base class for mostHttpMessageConverterimplementations.This base class adds support for setting supported
MediaTypes, through thesupportedMediaTypesbean property. It also adds support forContent-TypeandContent-Lengthwhen writing to output messages.- Since:
- 3.0
- Author:
- Arjen Poutsma, Juergen Hoeller, Sebastien Deleuze
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractHttpMessageConverter()Construct anAbstractHttpMessageConverterwith no supported media types.protectedAbstractHttpMessageConverter(Charset defaultCharset, MediaType... supportedMediaTypes)Construct anAbstractHttpMessageConverterwith a default charset and multiple supported media types.protectedAbstractHttpMessageConverter(MediaType supportedMediaType)Construct anAbstractHttpMessageConverterwith one supported media type.protectedAbstractHttpMessageConverter(MediaType... supportedMediaTypes)Construct anAbstractHttpMessageConverterwith multiple supported media types.
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected voidaddDefaultHeaders(HttpHeaders headers, T t, MediaType contentType)Add default headers to the output message.booleancanRead(Class<?> clazz, MediaType mediaType)This implementation checks if the given class is supported, and if the supported media typesinclude the given media type.protected booleancanRead(MediaType mediaType)booleancanWrite(Class<?> clazz, MediaType mediaType)protected booleancanWrite(MediaType mediaType)Returnstrueif the given media type includes any of the supported media types.protected LonggetContentLength(T t, MediaType contentType)Returns the content length for the given type.CharsetgetDefaultCharset()Return the default character set, if any.protected MediaTypegetDefaultContentType(T t)Returns the default content type for the given type.List<MediaType>getSupportedMediaTypes()Return the list ofMediaTypeobjects supported by this converter.Tread(Class<? extends T> clazz, HttpInputMessage inputMessage)This implementation simple delegates toreadInternal(Class, HttpInputMessage).protected abstract TreadInternal(Class<? extends T> clazz, HttpInputMessage inputMessage)Abstract template method that reads the actual object.voidsetDefaultCharset(Charset defaultCharset)Set the default character set, if any.voidsetSupportedMediaTypes(List<MediaType> supportedMediaTypes)Set the list ofMediaTypeobjects supported by this converter.protected abstract booleansupports(Class<?> clazz)Indicates whether the given class is supported by this converter.voidwrite(T t, MediaType contentType, HttpOutputMessage outputMessage)This implementation sets the default headers by callingaddDefaultHeaders(org.springframework.http.HttpHeaders, T, org.springframework.http.MediaType), and then callswriteInternal(T, org.springframework.http.HttpOutputMessage).protected abstract voidwriteInternal(T t, HttpOutputMessage outputMessage)Abstract template method that writes the actual body.
Constructor Detail
AbstractHttpMessageConverter
protected AbstractHttpMessageConverter()
Construct anAbstractHttpMessageConverterwith no supported media types.
AbstractHttpMessageConverter
protected AbstractHttpMessageConverter(MediaType supportedMediaType)
Construct anAbstractHttpMessageConverterwith one supported media type.- Parameters:
supportedMediaType- the supported media type
AbstractHttpMessageConverter
protected AbstractHttpMessageConverter(MediaType... supportedMediaTypes)
Construct anAbstractHttpMessageConverterwith multiple supported media types.- Parameters:
supportedMediaTypes- the supported media types
AbstractHttpMessageConverter
protected AbstractHttpMessageConverter(Charset defaultCharset, MediaType... supportedMediaTypes)
Construct anAbstractHttpMessageConverterwith a default charset and multiple supported media types.- Parameters:
defaultCharset- the default character setsupportedMediaTypes- the supported media types- Since:
- 4.3
Method Detail
setSupportedMediaTypes
public void setSupportedMediaTypes(List<MediaType> supportedMediaTypes)
Set the list ofMediaTypeobjects supported by this converter.
getSupportedMediaTypes
public List<MediaType> getSupportedMediaTypes()
Description copied from interface:HttpMessageConverterReturn the list ofMediaTypeobjects supported by this converter.- Specified by:
getSupportedMediaTypesin interfaceHttpMessageConverter<T>- Returns:
- the list of supported media types, potentially an immutable copy
setDefaultCharset
public void setDefaultCharset(Charset defaultCharset)
Set the default character set, if any.- Since:
- 4.3
getDefaultCharset
public Charset getDefaultCharset()
Return the default character set, if any.- Since:
- 4.3
canRead
public boolean canRead(Class<?> clazz, MediaType mediaType)
This implementation checks if the given class is supported, and if the supported media typesinclude the given media type.- Specified by:
canReadin interfaceHttpMessageConverter<T>- Parameters:
clazz- the class to test for readabilitymediaType- the media type to read (can benullif not specified); typically the value of aContent-Typeheader.- Returns:
trueif readable;falseotherwise
canRead
protected boolean canRead(MediaType mediaType)
- Parameters:
mediaType- the media type to read, can benullif not specified. Typically the value of aContent-Typeheader.- Returns:
trueif the supported media types include the media type, or if the media type isnull
canWrite
public boolean canWrite(Class<?> clazz, MediaType mediaType)
This implementation checks if the given class is supported, and if the supported media types include the given media type.- Specified by:
canWritein interfaceHttpMessageConverter<T>- Parameters:
clazz- the class to test for writabilitymediaType- the media type to write (can benullif not specified); typically the value of anAcceptheader.- Returns:
trueif writable;falseotherwise
canWrite
protected boolean canWrite(MediaType mediaType)
Returnstrueif the given media type includes any of the supported media types.- Parameters:
mediaType- the media type to write, can benullif not specified. Typically the value of anAcceptheader.- Returns:
trueif the supported media types are compatible with the media type, or if the media type isnull
read
public final T read(Class<? extends T> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException
This implementation simple delegates toreadInternal(Class, HttpInputMessage). Future implementations might add some default behavior, however.- Specified by:
readin interfaceHttpMessageConverter<T>- Parameters:
clazz- the type of object to return. This type must have previously been passed to thecanReadmethod of this interface, which must have returnedtrue.inputMessage- the HTTP input message to read from- Returns:
- the converted object
- Throws:
IOException- in case of I/O errorsHttpMessageNotReadableException- in case of conversion errors
write
public final void write(T t, MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException
This implementation sets the default headers by callingaddDefaultHeaders(org.springframework.http.HttpHeaders, T, org.springframework.http.MediaType), and then callswriteInternal(T, org.springframework.http.HttpOutputMessage).- Specified by:
writein interfaceHttpMessageConverter<T>- Parameters:
t- the object to write to the output message. The type of this object must have previously been passed to thecanWritemethod of this interface, which must have returnedtrue.contentType- the content type to use when writing. May benullto indicate that the default content type of the converter must be used. If notnull, this media type must have previously been passed to thecanWritemethod of this interface, which must have returnedtrue.outputMessage- the message to write to- Throws:
IOException- in case of I/O errorsHttpMessageNotWritableException- in case of conversion errors
addDefaultHeaders
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, callsgetContentLength(T, org.springframework.http.MediaType), and sets the corresponding headers.- Throws:
IOException- Since:
- 4.2
getDefaultContentType
protected MediaType getDefaultContentType(T t) throws IOException
Returns the default content type for the given type. Called whenwrite(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
supportedMediaTypesproperty, if any. Can be overridden in subclasses.- Parameters:
t- the type to return the content type for- Returns:
- the content type, or
nullif not known - Throws:
IOException
getContentLength
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- the type to return the content length for- Returns:
- the content length, or
nullif not known - Throws:
IOException
supports
protected abstract boolean supports(Class<?> clazz)
Indicates whether the given class is supported by this converter.- Parameters:
clazz- the class to test for support- Returns:
trueif supported;falseotherwise
readInternal
protected abstract T readInternal(Class<? extends T> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException
Abstract template method that reads the actual object. Invoked fromread(java.lang.Class<? extends T>, org.springframework.http.HttpInputMessage).- Parameters:
clazz- the type of object to returninputMessage- the HTTP input message to read from- Returns:
- the converted object
- Throws:
IOException- in case of I/O errorsHttpMessageNotReadableException- in case of conversion errors
writeInternal
protected abstract void writeInternal(T t, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException
Abstract template method that writes the actual body. Invoked fromwrite(T, org.springframework.http.MediaType, org.springframework.http.HttpOutputMessage).- Parameters:
t- the object to write to the output messageoutputMessage- the HTTP output message to write to- Throws:
IOException- in case of I/O errorsHttpMessageNotWritableException- in case of conversion errors