Top Description Methods
org.springframework.http.converter

public Interface GenericHttpMessageConverter<T>

extends HttpMessageConverter<T>
Known Direct Implementers
org.springframework.http.converter.xml.Jaxb2CollectionHttpMessageConverter, org.springframework.http.converter.AbstractGenericHttpMessageConverter
Type Parameters
<T>
the converted object type
Imports
java.io.IOException, java.lang.reflect.Type, org.springframework.http.HttpInputMessage, .HttpOutputMessage, .MediaType, org.springframework.lang.Nullable

A specialization of HttpMessageConverter that can convert an HTTP request into a target object of a specified generic type and a source object of a specified generic type into an HTTP response.
Authors
Arjen Poutsma, Rossen Stoyanchev, Sebastien Deleuze
Since
3.2
See Also
org.springframework.core.ParameterizedTypeReference

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

true if readable; false otherwise
canRead
(Type
the (potentially generic) type to test for readability
type
,
Class<?>
a context class for the target type, for example a class in which the target type appears in a method signature (can be null)
contextClass
,
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 type can be read by this converter.

public boolean

Returns:

true if writable; false otherwise
canWrite
(Type
the (potentially generic) type to test for writability (can be null if not specified)
type
,
Class<?>
the source object 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 T

Returns:

the converted object
read
(Type
the (potentially generic) type of object to return. This type must have previously been passed to the canRead method of this interface, which must have returned true.
type
,
Class<?>
a context class for the target type, for example a class in which the target type appears in a method signature (can be null)
contextClass
,
HttpInputMessage
the HTTP input message to read from
inputMessage
)

Read an object of the given type form 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
,
Type
the (potentially generic) type of object to write. This type must have previously been passed to the canWrite method of this interface, which must have returned true. Can be null if not specified.
type
,
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.

Inherited from org.springframework.http.converter.HttpMessageConverter:
canReadcanWritegetSupportedMediaTypesgetSupportedMediaTypesreadwrite

Method Detail

canReadback to summary
public boolean canRead(Type type, Class<?> contextClass, MediaType mediaType)

Indicates whether the given type can be read by this converter. This method should perform the same checks as HttpMessageConverter#canRead(Class, MediaType) with additional ones related to the generic type.

Parameters
type:Type

the (potentially generic) type to test for readability

contextClass:Class<?>

a context class for the target type, for example a class in which the target type appears in a method signature (can be null)

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(Type type, Class<?> clazz, MediaType mediaType)

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
type:Type

the (potentially generic) type to test for writability (can be null if not specified)

clazz:Class<?>

the source object 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

Since
4.2
readback to summary
public T read(Type type, Class<?> contextClass, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException

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

Parameters
type:Type

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

contextClass:Class<?>

a context class for the target type, for example a class in which the target type appears in a method signature (can be null)

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, Type type, 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.

type:Type

the (potentially generic) type of object to write. This type must have previously been passed to the canWrite method of this interface, which must have returned true. Can be null if not specified.

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
Since
4.2