Class MarshallingMessageConverter
- All Implemented Interfaces:
InitializingBean
,MessageConverter
MessageConverter
that uses a Marshaller
and Unmarshaller
.
Marshals an object to a BytesMessage
, or to a TextMessage
if the
targetType
is set to MessageType.TEXT
.
Unmarshals from a TextMessage
or BytesMessage
to an object.- Since:
- 3.0
- Author:
- Arjen Poutsma, Juergen Hoeller
-
Constructor Summary
ConstructorDescriptionMarshallingMessageConverter
(Marshaller marshaller) Construct a newMarshallingMessageConverter
with the givenMarshaller
set.MarshallingMessageConverter
(Marshaller marshaller, Unmarshaller unmarshaller) Construct a newMarshallingMessageConverter
with the given Marshaller and Unmarshaller. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Invoked by the containingBeanFactory
after it has set all bean properties and satisfiedBeanFactoryAware
,ApplicationContextAware
etc.fromMessage
(Message message) This implementation unmarshals the givenMessage
into an object.protected BytesMessage
marshalToBytesMessage
(Object object, Session session, Marshaller marshaller) Marshal the given object to aBytesMessage
.protected Message
marshalToMessage
(Object object, Session session, Marshaller marshaller, MessageType targetType) Template method that allows for custom message marshalling.protected TextMessage
marshalToTextMessage
(Object object, Session session, Marshaller marshaller) Marshal the given object to aTextMessage
.void
setMarshaller
(Marshaller marshaller) Set theMarshaller
to be used by this message converter.void
setTargetType
(MessageType targetType) void
setUnmarshaller
(Unmarshaller unmarshaller) Set theUnmarshaller
to be used by this message converter.This implementation marshals the given object to aTextMessage
orBytesMessage
.protected Object
unmarshalFromBytesMessage
(BytesMessage message, Unmarshaller unmarshaller) Unmarshal the givenBytesMessage
into an object.protected Object
unmarshalFromMessage
(Message message, Unmarshaller unmarshaller) Template method that allows for custom message unmarshalling.protected Object
unmarshalFromTextMessage
(TextMessage message, Unmarshaller unmarshaller) Unmarshal the givenTextMessage
into an object.
-
Constructor Details
-
MarshallingMessageConverter
public MarshallingMessageConverter()Construct a newMarshallingMessageConverter
with noMarshaller
orUnmarshaller
set. The marshaller must be set after construction by invokingsetMarshaller(Marshaller)
andsetUnmarshaller(Unmarshaller)
. -
MarshallingMessageConverter
Construct a newMarshallingMessageConverter
with the givenMarshaller
set.If the given
Marshaller
also implements theUnmarshaller
interface, it is used for both marshalling and unmarshalling. Otherwise, an exception is thrown.Note that all
Marshaller
implementations in Spring also implement theUnmarshaller
interface, so that you can safely use this constructor.- Parameters:
marshaller
- object used as marshaller and unmarshaller- Throws:
IllegalArgumentException
- whenmarshaller
does not implement theUnmarshaller
interface as well
-
MarshallingMessageConverter
Construct a newMarshallingMessageConverter
with the given Marshaller and Unmarshaller.- Parameters:
marshaller
- the Marshaller to useunmarshaller
- the Unmarshaller to use
-
-
Method Details
-
setMarshaller
Set theMarshaller
to be used by this message converter. -
setUnmarshaller
Set theUnmarshaller
to be used by this message converter. -
setTargetType
Specify whethertoMessage(Object, Session)
should marshal to aBytesMessage
or aTextMessage
.The default is
MessageType.BYTES
, i.e. this converter marshals to aBytesMessage
. Note that the default version of this converter supportsMessageType.BYTES
andMessageType.TEXT
only.- See Also:
-
afterPropertiesSet
public void afterPropertiesSet()Description copied from interface:InitializingBean
Invoked by the containingBeanFactory
after it has set all bean properties and satisfiedBeanFactoryAware
,ApplicationContextAware
etc.This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.
- Specified by:
afterPropertiesSet
in interfaceInitializingBean
-
toMessage
public Message toMessage(Object object, Session session) throws JMSException, MessageConversionException This implementation marshals the given object to aTextMessage
orBytesMessage
. The desired message type can be defined by setting the"marshalTo"
property.- Specified by:
toMessage
in interfaceMessageConverter
- Parameters:
object
- the object to convertsession
- the Session to use for creating a JMS Message- Returns:
- the JMS Message
- Throws:
JMSException
- if thrown by JMS API methodsMessageConversionException
- in case of conversion failure- See Also:
-
fromMessage
This implementation unmarshals the givenMessage
into an object.- Specified by:
fromMessage
in interfaceMessageConverter
- Parameters:
message
- the message to convert- Returns:
- the converted Java object
- Throws:
JMSException
- if thrown by JMS API methodsMessageConversionException
- in case of conversion failure- See Also:
-
marshalToTextMessage
protected TextMessage marshalToTextMessage(Object object, Session session, Marshaller marshaller) throws JMSException, IOException, XmlMappingException Marshal the given object to aTextMessage
.- Parameters:
object
- the object to be marshalledsession
- current JMS sessionmarshaller
- the marshaller to use- Returns:
- the resulting message
- Throws:
JMSException
- if thrown by JMS methodsIOException
- in case of I/O errorsXmlMappingException
- in case of OXM mapping errors- See Also:
-
marshalToBytesMessage
protected BytesMessage marshalToBytesMessage(Object object, Session session, Marshaller marshaller) throws JMSException, IOException, XmlMappingException Marshal the given object to aBytesMessage
.- Parameters:
object
- the object to be marshalledsession
- current JMS sessionmarshaller
- the marshaller to use- Returns:
- the resulting message
- Throws:
JMSException
- if thrown by JMS methodsIOException
- in case of I/O errorsXmlMappingException
- in case of OXM mapping errors- See Also:
-
marshalToMessage
protected Message marshalToMessage(Object object, Session session, Marshaller marshaller, MessageType targetType) throws JMSException, IOException, XmlMappingException Template method that allows for custom message marshalling. Invoked whensetTargetType(org.springframework.jms.support.converter.MessageType)
is notMessageType.TEXT
orMessageType.BYTES
.The default implementation throws an
IllegalArgumentException
.- Parameters:
object
- the object to marshalsession
- the JMS sessionmarshaller
- the marshaller to usetargetType
- the target message type (other than TEXT or BYTES)- Returns:
- the resulting message
- Throws:
JMSException
- if thrown by JMS methodsIOException
- in case of I/O errorsXmlMappingException
- in case of OXM mapping errors
-
unmarshalFromTextMessage
protected Object unmarshalFromTextMessage(TextMessage message, Unmarshaller unmarshaller) throws JMSException, IOException, XmlMappingException Unmarshal the givenTextMessage
into an object.- Parameters:
message
- the messageunmarshaller
- the unmarshaller to use- Returns:
- the unmarshalled object
- Throws:
JMSException
- if thrown by JMS methodsIOException
- in case of I/O errorsXmlMappingException
- in case of OXM mapping errors- See Also:
-
unmarshalFromBytesMessage
protected Object unmarshalFromBytesMessage(BytesMessage message, Unmarshaller unmarshaller) throws JMSException, IOException, XmlMappingException Unmarshal the givenBytesMessage
into an object.- Parameters:
message
- the messageunmarshaller
- the unmarshaller to use- Returns:
- the unmarshalled object
- Throws:
JMSException
- if thrown by JMS methodsIOException
- in case of I/O errorsXmlMappingException
- in case of OXM mapping errors- See Also:
-
unmarshalFromMessage
protected Object unmarshalFromMessage(Message message, Unmarshaller unmarshaller) throws JMSException, IOException, XmlMappingException Template method that allows for custom message unmarshalling. Invoked whenfromMessage(Message)
is invoked with a message that is not aTextMessage
orBytesMessage
.The default implementation throws an
IllegalArgumentException
.- Parameters:
message
- the messageunmarshaller
- the unmarshaller to use- Returns:
- the unmarshalled object
- Throws:
JMSException
- if thrown by JMS methodsIOException
- in case of I/O errorsXmlMappingException
- in case of OXM mapping errors
-