Class AbstractSmartHttpMessageConverter<T>
java.lang.Object
org.springframework.http.converter.AbstractHttpMessageConverter<T>
org.springframework.http.converter.AbstractSmartHttpMessageConverter<T>
- Type Parameters:
T
- the converted object type
- All Implemented Interfaces:
HttpMessageConverter<T>
,SmartHttpMessageConverter<T>
- Direct Known Subclasses:
AbstractKotlinSerializationHttpMessageConverter
public abstract class AbstractSmartHttpMessageConverter<T>
extends AbstractHttpMessageConverter<T>
implements SmartHttpMessageConverter<T>
Abstract base class for most
SmartHttpMessageConverter
implementations.- Since:
- 6.2
- Author:
- Sebastien Deleuze
-
Field Summary
Fields inherited from class org.springframework.http.converter.AbstractHttpMessageConverter
logger
-
Constructor Summary
ModifierConstructorDescriptionprotected
Construct anAbstractSmartHttpMessageConverter
with no supported media types.protected
AbstractSmartHttpMessageConverter
(MediaType supportedMediaType) Construct anAbstractSmartHttpMessageConverter
with one supported media type.protected
AbstractSmartHttpMessageConverter
(MediaType... supportedMediaTypes) Construct anAbstractSmartHttpMessageConverter
with multiple supported media type. -
Method Summary
Modifier and TypeMethodDescriptionboolean
canRead
(ResolvableType type, MediaType mediaType) Indicates whether the given type can be read by this converter.boolean
canWrite
(ResolvableType type, Class<?> clazz, MediaType mediaType) Indicates whether the given class can be written by this converter.protected T
readInternal
(Class<? extends T> clazz, HttpInputMessage inputMessage) Abstract template method that reads the actual object.protected boolean
Indicates whether the given class is supported by this converter.final void
write
(T t, ResolvableType type, MediaType contentType, HttpOutputMessage outputMessage, Map<String, Object> hints) 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, ResolvableType type, HttpOutputMessage outputMessage, Map<String, Object> hints) 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, setDefaultCharset, setSupportedMediaTypes, supportsRepeatableWrites, 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.HttpMessageConverter
getSupportedMediaTypes, getSupportedMediaTypes
-
Constructor Details
-
AbstractSmartHttpMessageConverter
protected AbstractSmartHttpMessageConverter()Construct anAbstractSmartHttpMessageConverter
with no supported media types. -
AbstractSmartHttpMessageConverter
Construct anAbstractSmartHttpMessageConverter
with one supported media type.- Parameters:
supportedMediaType
- the supported media type
-
AbstractSmartHttpMessageConverter
Construct anAbstractSmartHttpMessageConverter
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:SmartHttpMessageConverter
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 interfaceSmartHttpMessageConverter<T>
- Parameters:
type
- the (potentially generic) type to test for readability. The type source may be used for retrieving additional information (the related method signature for example) when relevant.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:SmartHttpMessageConverter
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 interfaceSmartHttpMessageConverter<T>
- Parameters:
type
- the (potentially generic) target type to test for writability (can beResolvableType.NONE
if not specified). The type source may be used for retrieving additional information (the related method signature for example) when relevant.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, ResolvableType type, @Nullable MediaType contentType, HttpOutputMessage outputMessage, @Nullable Map<String, Object> hints) throws IOException, HttpMessageNotWritableExceptionThis 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 interfaceSmartHttpMessageConverter<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 beResolvableType.NONE
if not specified. The type source may be used for retrieving additional information (the related method signature for example) when relevant.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 tohints
- additional information about how to encode- 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, ResolvableType type, HttpOutputMessage outputMessage, @Nullable Map<String, Object> hints) throws IOException, HttpMessageNotWritableExceptionAbstract template method that writes the actual body. Invoked fromwrite(Object, ResolvableType, MediaType, HttpOutputMessage, Map)
.- Parameters:
t
- the object to write to the output messagetype
- the type of object to writeoutputMessage
- the HTTP output message to write tohints
- additional information about how to encode- Throws:
IOException
- in case of I/O errorsHttpMessageNotWritableException
- in case of conversion errors
-
readInternal
protected T readInternal(Class<? extends T> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException Description copied from class:AbstractHttpMessageConverter
Abstract template method that reads the actual object. Invoked fromAbstractHttpMessageConverter.read(java.lang.Class<? extends T>, org.springframework.http.HttpInputMessage)
.- Specified by:
readInternal
in classAbstractHttpMessageConverter<T>
- 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
-