Class MarshallingHttpMessageConverter
- java.lang.Object
- org.springframework.http.converter.AbstractHttpMessageConverter<T>
- org.springframework.http.converter.xml.AbstractXmlHttpMessageConverter<Object>
- org.springframework.http.converter.xml.MarshallingHttpMessageConverter
- All Implemented Interfaces:
HttpMessageConverter<Object>
public class MarshallingHttpMessageConverter extends AbstractXmlHttpMessageConverter<Object>
Implementation ofHttpMessageConverterthat can read and write XML using Spring'sMarshallerandUnmarshallerabstractions.This converter requires a
MarshallerandUnmarshallerbefore it can be used. These can be injected by the constructor or bean properties.By default, this converter supports
text/xmlandapplication/xml. This can be overridden by setting thesupportedMediaTypesproperty.- Since:
- 3.0
- Author:
- Arjen Poutsma
Field Summary
Fields inherited from class org.springframework.http.converter.AbstractHttpMessageConverter
logger
Constructor Summary
Constructors Constructor Description MarshallingHttpMessageConverter()MarshallingHttpMessageConverter(Marshaller marshaller)Construct a newMarshallingMessageConverterwith the givenMarshallerset.MarshallingHttpMessageConverter(Marshaller marshaller, Unmarshaller unmarshaller)Construct a newMarshallingMessageConverterwith the givenMarshallerandUnmarshaller.
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleancanRead(Class<?> clazz, MediaType mediaType)This implementation checks if the given class is supported, and if the supported media typesinclude the given media type.booleancanWrite(Class<?> clazz, MediaType mediaType)protected ObjectreadFromSource(Class<?> clazz, HttpHeaders headers, Source source)Abstract template method called fromAbstractHttpMessageConverter.read(Class, HttpInputMessage).voidsetMarshaller(Marshaller marshaller)Set theMarshallerto be used by this message converter.voidsetUnmarshaller(Unmarshaller unmarshaller)Set theUnmarshallerto be used by this message converter.protected booleansupports(Class<?> clazz)Indicates whether the given class is supported by this converter.protected voidwriteToResult(Object o, HttpHeaders headers, Result result)Abstract template method called fromAbstractXmlHttpMessageConverter.writeInternal(Object, HttpOutputMessage).Methods inherited from class org.springframework.http.converter.xml.AbstractXmlHttpMessageConverter
readInternal, transform, writeInternal
Methods inherited from class org.springframework.http.converter.AbstractHttpMessageConverter
addDefaultHeaders, canRead, canWrite, getContentLength, getDefaultCharset, getDefaultContentType, getSupportedMediaTypes, read, setDefaultCharset, setSupportedMediaTypes, write
Constructor Detail
MarshallingHttpMessageConverter
public MarshallingHttpMessageConverter()
Construct a newMarshallingHttpMessageConverterwith noMarshallerorUnmarshallerset. The Marshaller and Unmarshaller must be set after construction by invokingsetMarshaller(Marshaller)andsetUnmarshaller(Unmarshaller).
MarshallingHttpMessageConverter
public MarshallingHttpMessageConverter(Marshaller marshaller)
Construct a newMarshallingMessageConverterwith the givenMarshallerset.If the given
Marshalleralso implements theUnmarshallerinterface, it is used for both marshalling and unmarshalling. Otherwise, an exception is thrown.Note that all
Marshallerimplementations in Spring also implement theUnmarshallerinterface, so that you can safely use this constructor.- Parameters:
marshaller- object used as marshaller and unmarshaller
MarshallingHttpMessageConverter
public MarshallingHttpMessageConverter(Marshaller marshaller, Unmarshaller unmarshaller)
Construct a newMarshallingMessageConverterwith the givenMarshallerandUnmarshaller.- Parameters:
marshaller- the Marshaller to useunmarshaller- the Unmarshaller to use
Method Detail
setMarshaller
public void setMarshaller(Marshaller marshaller)
Set theMarshallerto be used by this message converter.
setUnmarshaller
public void setUnmarshaller(Unmarshaller unmarshaller)
Set theUnmarshallerto be used by this message converter.
canRead
public boolean canRead(Class<?> clazz, MediaType mediaType)
Description copied from class:AbstractHttpMessageConverterThis implementation checks if the given class is supported, and if the supported media typesinclude the given media type.- Specified by:
canReadin interfaceHttpMessageConverter<Object>- Overrides:
canReadin classAbstractHttpMessageConverter<Object>- Parameters:
clazz- the class to test for readabilitymediaType- the media type to read (can benullif not specified); typically the value of aContent-Typeheader.- Returns:
trueif readable;falseotherwise
canWrite
public boolean canWrite(Class<?> clazz, MediaType mediaType)
Description copied from class:AbstractHttpMessageConverterThis implementation checks if the given class is supported, and if the supported media types include the given media type.- Specified by:
canWritein interfaceHttpMessageConverter<Object>- Overrides:
canWritein classAbstractHttpMessageConverter<Object>- Parameters:
clazz- the class to test for writabilitymediaType- the media type to write (can benullif not specified); typically the value of anAcceptheader.- Returns:
trueif writable;falseotherwise
supports
protected boolean supports(Class<?> clazz)
Description copied from class:AbstractHttpMessageConverterIndicates whether the given class is supported by this converter.- Specified by:
supportsin classAbstractHttpMessageConverter<Object>- Parameters:
clazz- the class to test for support- Returns:
trueif supported;falseotherwise
readFromSource
protected Object readFromSource(Class<?> clazz, HttpHeaders headers, Source source) throws IOException
Description copied from class:AbstractXmlHttpMessageConverterAbstract template method called fromAbstractHttpMessageConverter.read(Class, HttpInputMessage).- Specified by:
readFromSourcein classAbstractXmlHttpMessageConverter<Object>- Parameters:
clazz- the type of object to returnheaders- the HTTP input headerssource- the HTTP input body- Returns:
- the converted object
- Throws:
IOException- in case of I/O errors
writeToResult
protected void writeToResult(Object o, HttpHeaders headers, Result result) throws IOException
Description copied from class:AbstractXmlHttpMessageConverterAbstract template method called fromAbstractXmlHttpMessageConverter.writeInternal(Object, HttpOutputMessage).- Specified by:
writeToResultin classAbstractXmlHttpMessageConverter<Object>- Parameters:
o- the object to write to the output messageheaders- the HTTP output headersresult- the HTTP output body- Throws:
IOException- in case of I/O errors