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
ConstructorsConstructorDescriptionMarshallingMessageConverter(Marshaller marshaller) Construct a newMarshallingMessageConverterwith the givenMarshallerset.MarshallingMessageConverter(Marshaller marshaller, Unmarshaller unmarshaller) Construct a newMarshallingMessageConverterwith the given Marshaller and Unmarshaller. -
Method Summary
Modifier and TypeMethodDescriptionvoidInvoked by the containingBeanFactoryafter it has set all bean properties and satisfiedBeanFactoryAware,ApplicationContextAwareetc.fromMessage(Message message) This implementation unmarshals the givenMessageinto an object.protected BytesMessagemarshalToBytesMessage(Object object, Session session, Marshaller marshaller) Marshal the given object to aBytesMessage.protected MessagemarshalToMessage(Object object, Session session, Marshaller marshaller, MessageType targetType) Template method that allows for custom message marshalling.protected TextMessagemarshalToTextMessage(Object object, Session session, Marshaller marshaller) Marshal the given object to aTextMessage.voidsetMarshaller(Marshaller marshaller) Set theMarshallerto be used by this message converter.voidsetTargetType(MessageType targetType) voidsetUnmarshaller(Unmarshaller unmarshaller) Set theUnmarshallerto be used by this message converter.This implementation marshals the given object to aTextMessageorBytesMessage.protected ObjectunmarshalFromBytesMessage(BytesMessage message, Unmarshaller unmarshaller) Unmarshal the givenBytesMessageinto an object.protected ObjectunmarshalFromMessage(Message message, Unmarshaller unmarshaller) Template method that allows for custom message unmarshalling.protected ObjectunmarshalFromTextMessage(TextMessage message, Unmarshaller unmarshaller) Unmarshal the givenTextMessageinto an object.
-
Constructor Details
-
MarshallingMessageConverter
public MarshallingMessageConverter()Construct a newMarshallingMessageConverterwith noMarshallerorUnmarshallerset. The marshaller must be set after construction by invokingsetMarshaller(Marshaller)andsetUnmarshaller(Unmarshaller). -
MarshallingMessageConverter
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- Throws:
IllegalArgumentException- whenmarshallerdoes not implement theUnmarshallerinterface as well
-
MarshallingMessageConverter
Construct a newMarshallingMessageConverterwith the given Marshaller and Unmarshaller.- Parameters:
marshaller- the Marshaller to useunmarshaller- the Unmarshaller to use
-
-
Method Details
-
setMarshaller
Set theMarshallerto be used by this message converter. -
setUnmarshaller
Set theUnmarshallerto be used by this message converter. -
setTargetType
Specify whethertoMessage(Object, Session)should marshal to aBytesMessageor aTextMessage.The default is
MessageType.BYTES, i.e. this converter marshals to aBytesMessage. Note that the default version of this converter supportsMessageType.BYTESandMessageType.TEXTonly.- See Also:
-
afterPropertiesSet
public void afterPropertiesSet()Description copied from interface:InitializingBeanInvoked by the containingBeanFactoryafter it has set all bean properties and satisfiedBeanFactoryAware,ApplicationContextAwareetc.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:
afterPropertiesSetin interfaceInitializingBean
-
toMessage
public Message toMessage(Object object, Session session) throws JMSException, MessageConversionException This implementation marshals the given object to aTextMessageorBytesMessage. The desired message type can be defined by setting the"marshalTo"property.- Specified by:
toMessagein 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 givenMessageinto an object.- Specified by:
fromMessagein 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.TEXTorMessageType.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 givenTextMessageinto 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 givenBytesMessageinto 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 aTextMessageorBytesMessage.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
-