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:
AbstractJacksonHttpMessageConverter, 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 AbstractHttpMessageConverter
logger -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedConstruct anAbstractSmartHttpMessageConverterwith no supported media types.protectedAbstractSmartHttpMessageConverter(MediaType supportedMediaType) Construct anAbstractSmartHttpMessageConverterwith one supported media type.protectedAbstractSmartHttpMessageConverter(MediaType... supportedMediaTypes) Construct anAbstractSmartHttpMessageConverterwith multiple supported media type. -
Method Summary
Modifier and TypeMethodDescriptionbooleancanRead(ResolvableType type, @Nullable MediaType mediaType) Indicates whether the given type can be read by this converter.booleancanWrite(ResolvableType type, Class<?> clazz, @Nullable MediaType mediaType) Indicates whether the given class can be written by this converter.protected TreadInternal(Class<? extends T> clazz, HttpInputMessage inputMessage) Abstract template method that reads the actual object.protected booleanIndicates whether the given class is supported by this converter.final voidwrite(T t, ResolvableType type, @Nullable MediaType contentType, HttpOutputMessage outputMessage, @Nullable Map<String, Object> hints) This implementation sets the default headers by callingAbstractHttpMessageConverter.addDefaultHeaders(HttpHeaders, T, MediaType), and then callswriteInternal(T, HttpOutputMessage).protected abstract voidwriteInternal(T t, ResolvableType type, HttpOutputMessage outputMessage, @Nullable Map<String, Object> hints) Abstract template method that writes the actual body.protected voidwriteInternal(T t, HttpOutputMessage outputMessage) Abstract template method that writes the actual body.Methods inherited from class AbstractHttpMessageConverter
addDefaultHeaders, canRead, canRead, canWrite, canWrite, getContentLength, getDefaultCharset, getDefaultContentType, getSupportedMediaTypes, read, setDefaultCharset, setSupportedMediaTypes, supportsRepeatableWrites, writeMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface HttpMessageConverter
getSupportedMediaTypes, getSupportedMediaTypes
-
Constructor Details
-
AbstractSmartHttpMessageConverter
protected AbstractSmartHttpMessageConverter()Construct anAbstractSmartHttpMessageConverterwith no supported media types.- See Also:
-
AbstractSmartHttpMessageConverter
Construct anAbstractSmartHttpMessageConverterwith one supported media type.- Parameters:
supportedMediaType- the supported media type
-
AbstractSmartHttpMessageConverter
Construct anAbstractSmartHttpMessageConverterwith multiple supported media type.- Parameters:
supportedMediaTypes- the supported media types
-
-
Method Details
-
supports
Description copied from class:AbstractHttpMessageConverterIndicates whether the given class is supported by this converter.- Specified by:
supportsin classAbstractHttpMessageConverter<T>- Parameters:
clazz- the class to test for support- Returns:
trueif supported;falseotherwise
-
canRead
Description copied from interface:SmartHttpMessageConverterIndicates 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:
canReadin 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 benullif not specified. Typically, the value of aContent-Typeheader.- Returns:
trueif readable;falseotherwise
-
canWrite
Description copied from interface:SmartHttpMessageConverterIndicates 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:
canWritein interfaceSmartHttpMessageConverter<T>- Parameters:
type- the (potentially generic) target type to test for writability (can beResolvableType.NONEif 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 benullif not specified); typically the value of anAcceptheader.- Returns:
trueif writable;falseotherwise
-
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(HttpHeaders, T, MediaType), and then callswriteInternal(T, HttpOutputMessage).- Specified by:
writein interfaceSmartHttpMessageConverter<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.type- the (potentially generic) type of object to write. This type must have previously been passed to thecanWritemethod of this interface, which must have returnedtrue. Can beResolvableType.NONEif 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 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 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:AbstractHttpMessageConverterAbstract template method that writes the actual body. Invoked fromAbstractHttpMessageConverter.write(T, MediaType, HttpOutputMessage).- Specified by:
writeInternalin 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:AbstractHttpMessageConverterAbstract template method that reads the actual object. Invoked fromAbstractHttpMessageConverter.read(Class, HttpInputMessage).- Specified by:
readInternalin 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
-