Interface SmartHttpMessageConverter<T>
- Type Parameters:
T
- the converted object type
- All Superinterfaces:
HttpMessageConverter<T>
- All Known Implementing Classes:
AbstractKotlinSerializationHttpMessageConverter
,AbstractSmartHttpMessageConverter
,KotlinSerializationBinaryHttpMessageConverter
,KotlinSerializationCborHttpMessageConverter
,KotlinSerializationJsonHttpMessageConverter
,KotlinSerializationProtobufHttpMessageConverter
,KotlinSerializationStringHttpMessageConverter
A specialization of
HttpMessageConverter
that can convert an HTTP request
into a target object of a specified ResolvableType
and a source object of
a specified ResolvableType
into an HTTP response with optional hints.
It provides default methods for HttpMessageConverter
in order to allow
subclasses to only have to implement the smart APIs.
- Since:
- 6.2
- Author:
- Sebastien Deleuze
-
Method Summary
Modifier and TypeMethodDescriptiondefault boolean
Indicates whether the given class can be read by this converter.boolean
canRead
(ResolvableType type, MediaType mediaType) Indicates whether the given type can be read by this converter.default boolean
Indicates whether the given class can be written by this converter.boolean
canWrite
(ResolvableType targetType, Class<?> valueClass, MediaType mediaType) Indicates whether the given class can be written by this converter.default T
read
(Class<? extends T> clazz, HttpInputMessage inputMessage) Read an object of the given type from the given input message, and returns it.read
(ResolvableType type, HttpInputMessage inputMessage, Map<String, Object> hints) Read an object of the given type from the given input message, and returns it.void
write
(T t, ResolvableType type, MediaType contentType, HttpOutputMessage outputMessage, Map<String, Object> hints) Write a given object to the given output message.default void
write
(T t, MediaType contentType, HttpOutputMessage outputMessage) Write a given object to the given output message.Methods inherited from interface org.springframework.http.converter.HttpMessageConverter
getSupportedMediaTypes, getSupportedMediaTypes
-
Method Details
-
canRead
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.- 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
-
canRead
Description copied from interface:HttpMessageConverter
Indicates whether the given class can be read by this converter.- Specified by:
canRead
in interfaceHttpMessageConverter<T>
- Parameters:
clazz
- the class to test for readabilitymediaType
- the media type to read (can benull
if not specified); typically the value of aContent-Type
header.- Returns:
true
if readable;false
otherwise
-
read
T read(ResolvableType type, HttpInputMessage inputMessage, @Nullable Map<String, Object> hints) throws IOException, HttpMessageNotReadableExceptionRead an object of the given type from the given input message, and returns it.- Parameters:
type
- the (potentially generic) type of object to return. This type must have previously been passed to thecanRead
method of this interface, which must have returnedtrue
. The type source may be used for retrieving additional information (the related method signature for example) when relevant.inputMessage
- the HTTP input message to read fromhints
- additional information about how to encode- Returns:
- the converted object
- Throws:
IOException
- in case of I/O errorsHttpMessageNotReadableException
- in case of conversion errors
-
read
default T read(Class<? extends T> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException Description copied from interface:HttpMessageConverter
Read an object of the given type from the given input message, and returns it.- Specified by:
read
in interfaceHttpMessageConverter<T>
- Parameters:
clazz
- the type of object to return. This type must have previously been passed to thecanRead
method 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
-
canWrite
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.- Parameters:
targetType
- 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.valueClass
- 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
-
canWrite
Description copied from interface:HttpMessageConverter
Indicates whether the given class can be written by this converter.- Specified by:
canWrite
in interfaceHttpMessageConverter<T>
- Parameters:
clazz
- the 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
void write(T t, ResolvableType type, @Nullable MediaType contentType, HttpOutputMessage outputMessage, @Nullable Map<String, Object> hints) throws IOException, HttpMessageNotWritableExceptionWrite a given object to the given output message.- 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
-
write
default void write(T t, @Nullable MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException Description copied from interface:HttpMessageConverter
Write a given object to the given output message.- Specified by:
write
in interfaceHttpMessageConverter<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
.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
-