Class AbstractGenericHttpMessageConverter<T>
java.lang.Object
org.springframework.http.converter.AbstractHttpMessageConverter<T>
org.springframework.http.converter.AbstractGenericHttpMessageConverter<T>
- Type Parameters:
T
- the converted object type
- All Implemented Interfaces:
GenericHttpMessageConverter<T>
,HttpMessageConverter<T>
- Direct Known Subclasses:
AbstractJackson2HttpMessageConverter
,AbstractJsonHttpMessageConverter
,AbstractKotlinSerializationHttpMessageConverter
,ResourceRegionHttpMessageConverter
public abstract class AbstractGenericHttpMessageConverter<T>
extends AbstractHttpMessageConverter<T>
implements GenericHttpMessageConverter<T>
Abstract base class for most
GenericHttpMessageConverter
implementations.- Since:
- 4.2
- Author:
- Sebastien Deleuze, Juergen Hoeller
-
Field Summary
Fields inherited from class org.springframework.http.converter.AbstractHttpMessageConverter
logger
-
Constructor Summary
ModifierConstructorDescriptionprotected
Construct anAbstractGenericHttpMessageConverter
with no supported media types.protected
AbstractGenericHttpMessageConverter
(MediaType supportedMediaType) Construct anAbstractGenericHttpMessageConverter
with one supported media type.protected
AbstractGenericHttpMessageConverter
(MediaType... supportedMediaTypes) Construct anAbstractGenericHttpMessageConverter
with multiple supported media type. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Indicates whether the given type can be read by this converter.boolean
Indicates whether the given class can be written by this converter.protected boolean
Indicates whether the given class is supported by this converter.final void
write
(T t, Type type, MediaType contentType, HttpOutputMessage outputMessage) This implementation sets the default headers by callingAbstractHttpMessageConverter.addDefaultHeaders(org.springframework.http.HttpHeaders, T, org.springframework.http.MediaType)
, and then callswriteInternal(T, org.springframework.http.HttpOutputMessage)
.protected abstract void
writeInternal
(T t, Type type, HttpOutputMessage outputMessage) Abstract template method that writes the actual body.protected void
writeInternal
(T t, HttpOutputMessage outputMessage) Abstract template method that writes the actual body.Methods inherited from class org.springframework.http.converter.AbstractHttpMessageConverter
addDefaultHeaders, canRead, canRead, canWrite, canWrite, getContentLength, getDefaultCharset, getDefaultContentType, getSupportedMediaTypes, read, readInternal, setDefaultCharset, setSupportedMediaTypes, write
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.springframework.http.converter.GenericHttpMessageConverter
read
Methods inherited from interface org.springframework.http.converter.HttpMessageConverter
canRead, canWrite, getSupportedMediaTypes, getSupportedMediaTypes, read, write
-
Constructor Details
-
AbstractGenericHttpMessageConverter
protected AbstractGenericHttpMessageConverter()Construct anAbstractGenericHttpMessageConverter
with no supported media types. -
AbstractGenericHttpMessageConverter
Construct anAbstractGenericHttpMessageConverter
with one supported media type.- Parameters:
supportedMediaType
- the supported media type
-
AbstractGenericHttpMessageConverter
Construct anAbstractGenericHttpMessageConverter
with multiple supported media type.- Parameters:
supportedMediaTypes
- the supported media types
-
-
Method Details
-
supports
Description copied from class:AbstractHttpMessageConverter
Indicates whether the given class is supported by this converter.- Specified by:
supports
in classAbstractHttpMessageConverter<T>
- Parameters:
clazz
- the class to test for support- Returns:
true
if supported;false
otherwise
-
canRead
Description copied from interface:GenericHttpMessageConverter
Indicates whether the given type can be read by this converter. This method should perform the same checks asHttpMessageConverter.canRead(Class, MediaType)
with additional ones related to the generic type.- Specified by:
canRead
in interfaceGenericHttpMessageConverter<T>
- Parameters:
type
- the (potentially generic) type to test for readabilitycontextClass
- a context class for the target type, for example a class in which the target type appears in a method signature (can benull
)mediaType
- the media type to read, can benull
if not specified. Typically, the value of aContent-Type
header.- Returns:
true
if readable;false
otherwise
-
canWrite
Description copied from interface:GenericHttpMessageConverter
Indicates whether the given class can be written by this converter.This method should perform the same checks as
HttpMessageConverter.canWrite(Class, MediaType)
with additional ones related to the generic type.- Specified by:
canWrite
in interfaceGenericHttpMessageConverter<T>
- Parameters:
type
- the (potentially generic) type to test for writability (can benull
if not specified)clazz
- the source object class to test for writabilitymediaType
- the media type to write (can benull
if not specified); typically the value of anAccept
header.- Returns:
true
if writable;false
otherwise
-
write
public final void write(T t, @Nullable Type type, @Nullable MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException This implementation sets the default headers by callingAbstractHttpMessageConverter.addDefaultHeaders(org.springframework.http.HttpHeaders, T, org.springframework.http.MediaType)
, and then callswriteInternal(T, org.springframework.http.HttpOutputMessage)
.- Specified by:
write
in interfaceGenericHttpMessageConverter<T>
- Parameters:
t
- the object to write to the output message. The type of this object must have previously been passed to thecanWrite
method of this interface, which must have returnedtrue
.type
- the (potentially generic) type of object to write. This type must have previously been passed to thecanWrite
method of this interface, which must have returnedtrue
. Can benull
if not specified.contentType
- the content type to use when writing. May benull
to indicate that the default content type of the converter must be used. If notnull
, this media type must have previously been passed to thecanWrite
method 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
-
writeInternal
protected void writeInternal(T t, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException Description copied from class:AbstractHttpMessageConverter
Abstract template method that writes the actual body. Invoked fromAbstractHttpMessageConverter.write(T, org.springframework.http.MediaType, org.springframework.http.HttpOutputMessage)
.- Specified by:
writeInternal
in classAbstractHttpMessageConverter<T>
- 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
-
writeInternal
protected abstract void writeInternal(T t, @Nullable Type type, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException Abstract template method that writes the actual body. Invoked fromwrite(T, java.lang.reflect.Type, org.springframework.http.MediaType, org.springframework.http.HttpOutputMessage)
.- Parameters:
t
- the object to write to the output messagetype
- the type of object to write (may benull
)outputMessage
- the HTTP output message to write to- Throws:
IOException
- in case of I/O errorsHttpMessageNotWritableException
- in case of conversion errors
-