Top Description Fields Constructors Methods
org.springframework.http.converter.xml

public Class Jaxb2CollectionHttpMessageConverter<T extends java.util.Collection<E>>

extends AbstractJaxb2HttpMessageConverter<T extends Collection<E>>
implements GenericHttpMessageConverter<T extends Collection<E>>
Class Inheritance
All Implemented Interfaces
org.springframework.http.converter.GenericHttpMessageConverter, org.springframework.http.converter.HttpMessageConverter
Annotations
@SuppressWarnings:rawtypes
Type Parameters
<T>
the converted object type
Imports
java.io.IOException, java.lang.reflect.ParameterizedType, .Type, java.util.ArrayList, .Collection, .LinkedHashSet, .List, .SortedSet, .TreeSet, javax.xml.stream.XMLInputFactory, .XMLStreamException, .XMLStreamReader, javax.xml.transform.Result, .Source, jakarta.xml.bind.JAXBException, .UnmarshalException, .Unmarshaller, jakarta.xml.bind.annotation.XmlRootElement, .XmlType, org.springframework.http.HttpHeaders, .HttpInputMessage, .HttpOutputMessage, .MediaType, org.springframework.http.converter.GenericHttpMessageConverter, .HttpMessageConversionException, .HttpMessageNotReadableException, .HttpMessageNotWritableException, org.springframework.lang.Nullable, org.springframework.util.ReflectionUtils, org.springframework.util.xml.StaxUtils

An HttpMessageConverter that can read XML collections using JAXB2.

This converter can read collections that contain classes annotated with XmlRootElement and XmlType. Note that this converter does not support writing.

Authors
Arjen Poutsma, Rossen Stoyanchev, Sam Brannen
Since
3.2

Field Summary

Modifier and TypeField and Description
private final XMLInputFactory

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public boolean
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
)

Overrides org.springframework.http.converter.AbstractHttpMessageConverter.canRead.

Implements org.springframework.http.converter.HttpMessageConverter.canRead.

Always returns false since Jaxb2CollectionHttpMessageConverter required generic type information in order to read a Collection.
public boolean
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
)

Implements org.springframework.http.converter.GenericHttpMessageConverter.canRead.

Indicates whether the given type can be read by this converter.
public boolean
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
)

Overrides org.springframework.http.converter.AbstractHttpMessageConverter.canWrite.

Implements org.springframework.http.converter.HttpMessageConverter.canWrite.

Always returns false since Jaxb2CollectionHttpMessageConverter does not convert collections to XML.
public boolean
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
)

Implements org.springframework.http.converter.GenericHttpMessageConverter.canWrite.

Always returns false since Jaxb2CollectionHttpMessageConverter does not convert collections to XML.
protected T

Returns:

the created Collection instance
createCollection
(Class<?>
the type of Collection to instantiate
collectionClass
)

Create a Collection of the given type, with the given initial capacity (if supported by the Collection type).

protected XMLInputFactory
createXmlInputFactory()

Create an XMLInputFactory that this converter will use to create javax.xml.stream.XMLStreamReader and javax.xml.stream.XMLEventReader objects.

private int
private int
public T
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
)

Implements org.springframework.http.converter.GenericHttpMessageConverter.read.

Read an object of the given type form the given input message, and returns it.
protected T
readFromSource(Class<? extends T>
the type of object to return
clazz
,
HttpHeaders
the HTTP input headers
headers
,
Source
the HTTP input body
source
)

Implements abstract org.springframework.http.converter.xml.AbstractXmlHttpMessageConverter.readFromSource.

Abstract template method called from read(Class, HttpInputMessage).
protected boolean
supports(Class<?>
the class to test for support
clazz
)

Implements abstract org.springframework.http.converter.AbstractHttpMessageConverter.supports.

Indicates whether the given class is supported by this converter.
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
)

Implements org.springframework.http.converter.GenericHttpMessageConverter.write.

Write a given object to the given output message.
protected void
writeToResult(T
the object to write to the output message
t
,
HttpHeaders
the HTTP output headers
headers
,
Result
the HTTP output body
result
)

Implements abstract org.springframework.http.converter.xml.AbstractXmlHttpMessageConverter.writeToResult.

Abstract template method called from writeInternal(Object, HttpOutputMessage).
Inherited from org.springframework.http.converter.xml.AbstractJaxb2HttpMessageConverter:
createMarshallercreateUnmarshallercustomizeMarshallercustomizeUnmarshallergetJaxbContext

Field Detail

inputFactoryback to summary
private final XMLInputFactory inputFactory

Constructor Detail

Jaxb2CollectionHttpMessageConverterback to summary
public Jaxb2CollectionHttpMessageConverter()

Method Detail

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

Overrides org.springframework.http.converter.AbstractHttpMessageConverter.canRead.

Implements org.springframework.http.converter.HttpMessageConverter.canRead.

Always returns false since Jaxb2CollectionHttpMessageConverter required generic type information in order to read a Collection.

Parameters
clazz:Class<?>

Doc from org.springframework.http.converter.HttpMessageConverter.canRead.

the class to test for readability

mediaType:MediaType

Doc from org.springframework.http.converter.HttpMessageConverter.canRead.

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

Returns:boolean

Doc from org.springframework.http.converter.HttpMessageConverter.canRead.

true if readable; false otherwise

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

Implements org.springframework.http.converter.GenericHttpMessageConverter.canRead.

Doc from org.springframework.http.converter.GenericHttpMessageConverter.canRead.

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.

Jaxb2CollectionHttpMessageConverter can read a generic Collection where the generic type is a JAXB type annotated with XmlRootElement or XmlType.

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

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

Overrides org.springframework.http.converter.AbstractHttpMessageConverter.canWrite.

Implements org.springframework.http.converter.HttpMessageConverter.canWrite.

Always returns false since Jaxb2CollectionHttpMessageConverter does not convert collections to XML.

Parameters
clazz:Class<?>

Doc from org.springframework.http.converter.HttpMessageConverter.canWrite.

the class to test for writability

mediaType:MediaType

Doc from org.springframework.http.converter.HttpMessageConverter.canWrite.

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

Returns:boolean

Doc from org.springframework.http.converter.HttpMessageConverter.canWrite.

true if writable; false otherwise

Annotations
@Override
canWriteback to summary
public boolean canWrite(Type type, Class<?> clazz, MediaType mediaType)

Implements org.springframework.http.converter.GenericHttpMessageConverter.canWrite.

Always returns false since Jaxb2CollectionHttpMessageConverter does not convert collections to XML.

Parameters
type:Type

Doc from org.springframework.http.converter.GenericHttpMessageConverter.canWrite.

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

clazz:Class<?>

Doc from org.springframework.http.converter.GenericHttpMessageConverter.canWrite.

the source object class to test for writability

mediaType:MediaType

Doc from org.springframework.http.converter.GenericHttpMessageConverter.canWrite.

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

Returns:boolean

Doc from org.springframework.http.converter.GenericHttpMessageConverter.canWrite.

true if writable; false otherwise

Annotations
@Override
createCollectionback to summary
protected T createCollection(Class<?> collectionClass)

Create a Collection of the given type, with the given initial capacity (if supported by the Collection type).

Parameters
collectionClass:Class<?>

the type of Collection to instantiate

Returns:T

the created Collection instance

Annotations
@SuppressWarnings:unchecked
createXmlInputFactoryback to summary
protected XMLInputFactory createXmlInputFactory()

Create an XMLInputFactory that this converter will use to create javax.xml.stream.XMLStreamReader and javax.xml.stream.XMLEventReader objects.

Can be overridden in subclasses, adding further initialization of the factory. The resulting factory is cached, so this method will only be called once.

See Also
StaxUtils#createDefensiveInputFactory()
moveToFirstChildOfRootElementback to summary
private int moveToFirstChildOfRootElement(XMLStreamReader streamReader) throws XMLStreamException
moveToNextElementback to summary
private int moveToNextElement(XMLStreamReader streamReader) throws XMLStreamException
readback to summary
public T read(Type type, Class<?> contextClass, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException

Implements org.springframework.http.converter.GenericHttpMessageConverter.read.

Doc from org.springframework.http.converter.GenericHttpMessageConverter.read.

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

Annotations
@Override
@SuppressWarnings:unchecked
Exceptions
IOException:
in case of I/O errors
HttpMessageNotReadableException:
in case of conversion errors
readFromSourceback to summary
protected T readFromSource(Class<? extends T> clazz, HttpHeaders headers, Source source) throws Exception

Implements abstract org.springframework.http.converter.xml.AbstractXmlHttpMessageConverter.readFromSource.

Doc from org.springframework.http.converter.xml.AbstractXmlHttpMessageConverter.readFromSource.

Abstract template method called from read(Class, HttpInputMessage).

Parameters
clazz:Class<? extends T>

the type of object to return

headers:HttpHeaders

the HTTP input headers

source:Source

the HTTP input body

Returns:T

the converted object

Annotations
@Override
Exceptions
Exception:
in case of I/O or conversion errors
supportsback to summary
protected boolean supports(Class<?> clazz)

Implements abstract org.springframework.http.converter.AbstractHttpMessageConverter.supports.

Doc from org.springframework.http.converter.AbstractHttpMessageConverter.supports.

Indicates whether the given class is supported by this converter.

Parameters
clazz:Class<?>

the class to test for support

Returns:boolean

true if supported; false otherwise

Annotations
@Override
writeback to summary
public void write(T t, Type type, MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException

Implements org.springframework.http.converter.GenericHttpMessageConverter.write.

Doc from org.springframework.http.converter.GenericHttpMessageConverter.write.

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

Annotations
@Override
Exceptions
IOException:
in case of I/O errors
HttpMessageNotWritableException:
in case of conversion errors
writeToResultback to summary
protected void writeToResult(T t, HttpHeaders headers, Result result) throws Exception

Implements abstract org.springframework.http.converter.xml.AbstractXmlHttpMessageConverter.writeToResult.

Doc from org.springframework.http.converter.xml.AbstractXmlHttpMessageConverter.writeToResult.

Abstract template method called from writeInternal(Object, HttpOutputMessage).

Parameters
t:T

the object to write to the output message

headers:HttpHeaders

the HTTP output headers

result:Result

the HTTP output body

Annotations
@Override
Exceptions
Exception:
in case of I/O or conversion errors