Top Description Methods
org.springframework.http.converter

public Interface HttpMessageConverter<T>

Known Direct Subinterfaces
org.springframework.http.converter.GenericHttpMessageConverter
Known Direct Implementers
org.springframework.http.converter.AbstractHttpMessageConverter, org.springframework.http.converter.BufferedImageHttpMessageConverter, org.springframework.http.converter.FormHttpMessageConverter
Type Parameters
<T>
the converted object type
Imports
java.io.IOException, java.util.Collections, .List, org.springframework.http.HttpInputMessage, .HttpOutputMessage, .MediaType, org.springframework.lang.Nullable

Strategy interface for converting from and to HTTP requests and responses.
Authors
Arjen Poutsma, Juergen Hoeller, Rossen Stoyanchev
Since
3.0

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

true if readable; false otherwise
canRead
(Class<?>
the class to test for readability
clazz
,
MediaType
the media type to read (can be null if not specified); typically the value of a Content-Type header.
mediaType
)

Indicates whether the given class can be read by this converter.

public boolean

Returns:

true if writable; false otherwise
canWrite
(Class<?>
the class to test for writability
clazz
,
MediaType
the media type to write (can be null if not specified); typically the value of an Accept header.
mediaType
)

Indicates whether the given class can be written by this converter.

public List<MediaType>

Returns:

the list of supported media types
getSupportedMediaTypes
()

Return the list of media types supported by this converter.

public default List<MediaType>

Returns:

the list of media types supported for the given class
getSupportedMediaTypes
(Class<?>
the type of class to check
clazz
)

Return the list of media types supported by this converter for the given class.

public T

Returns:

the converted object
read
(Class<? extends T>
the type of object to return. This type must have previously been passed to the canRead method of this interface, which must have returned true.
clazz
,
HttpInputMessage
the HTTP input message to read from
inputMessage
)

Read an object of the given type from the given input message, and returns it.

public void
write(T
the object to write to the output message. The type of this object must have previously been passed to the canWrite method of this interface, which must have returned true.
t
,
MediaType
the content type to use when writing. May be null to indicate that the default content type of the converter must be used. If not null, this media type must have previously been passed to the canWrite method of this interface, which must have returned true.
contentType
,
HttpOutputMessage
the message to write to
outputMessage
)

Write a given object to the given output message.

Method Detail

canReadback to summary
public boolean canRead(Class<?> clazz, MediaType mediaType)

Indicates whether the given class can be read by this converter.

Parameters
clazz:Class<?>

the class to test for readability

mediaType:MediaType

the media type to read (can be null if not specified); typically the value of a Content-Type header.

Returns:boolean

true if readable; false otherwise

canWriteback to summary
public boolean canWrite(Class<?> clazz, MediaType mediaType)

Indicates whether the given class can be written by this converter.

Parameters
clazz:Class<?>

the class to test for writability

mediaType:MediaType

the media type to write (can be null if not specified); typically the value of an Accept header.

Returns:boolean

true if writable; false otherwise

getSupportedMediaTypesback to summary
public List<MediaType> getSupportedMediaTypes()

Return the list of media types supported by this converter. The list may not apply to every possible target element type and calls to this method should typically be guarded via canWrite(clazz, null. The list may also exclude MIME types supported only for a specific class. Alternatively, use getSupportedMediaTypes(Class) for a more precise list.

Returns:List<MediaType>

the list of supported media types

getSupportedMediaTypesback to summary
public default List<MediaType> getSupportedMediaTypes(Class<?> clazz)

Return the list of media types supported by this converter for the given class. The list may differ from getSupportedMediaTypes() if the converter does not support the given Class or if it supports it only for a subset of media types.

Parameters
clazz:Class<?>

the type of class to check

Returns:List<MediaType>

the list of media types supported for the given class

Since
5.3.4
readback to summary
public T read(Class<? extends T> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException

Read an object of the given type from the given input message, and returns it.

Parameters
clazz:Class<? extends T>

the type of object to return. This type must have previously been passed to the canRead method of this interface, which must have returned true.

inputMessage:HttpInputMessage

the HTTP input message to read from

Returns:T

the converted object

Exceptions
IOException:
in case of I/O errors
HttpMessageNotReadableException:
in case of conversion errors
writeback to summary
public void write(T t, MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException

Write a given object to the given output message.

Parameters
t:T

the object to write to the output message. The type of this object must have previously been passed to the canWrite method of this interface, which must have returned true.

contentType:MediaType

the content type to use when writing. May be null to indicate that the default content type of the converter must be used. If not null, this media type must have previously been passed to the canWrite method of this interface, which must have returned true.

outputMessage:HttpOutputMessage

the message to write to

Exceptions
IOException:
in case of I/O errors
HttpMessageNotWritableException:
in case of conversion errors