Ever thought what is the hardest part in dealing with SOAP messages ? You are right, Converting to and from SOAPMessage to Java Object and vice versa, especially when you get the SOAPMessage in a String object.
Converting SOAPMessages to Java Object's is no longer a complex task once you read through this post. You might have seen tons of articles on this topic but ever seen the examples used ? Yes!! they are simple XML messages with no attributes, no namespaces, no namespace prefixes. We call these messages as 'Hello World XMLs'. In real world, your XMLs are not so simple.
We promise, we wont be dealing with 'Hello World XMLs'. We will explain the converting using an XML message with attributes, namespaces, namespace prefix, header with namespace, namespace prefix etc.
Our Test XML, complex isn't it ?
loading...
Way 1: SOAPMessage to Java Object Using Jaxb2Marshaller
1. Configure Jaxb2Marshaller Bean
loading...
2. Autowire Jaxb2Marshaller Bean
loading...
3. Convert to SOAPMessage
This is the one of the most important steps, Observe, how the namespace prefix and namespace URIs are used in both the header and the body. This is very useful when you want to add some tag to the header. Ideally, most of the financial institutions and secure systems uses this kind of headers.
loading...
4. UnMarshall to Java Object
loading...
5. Response Java Object
This is the most important steps, if you make any mistake here, you will lose a lot of time debugging the different kinds of exceptions. So be very careful while defining Response object.
loading...
Output:
--- Print SOAPMessage as String ---
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:lij="http://www.learninjava.com/soaptoobject/1_0/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Header>
<lijcommon:LIJHeader xmlns:lijcommon="http://www.learninjava.com/common/1_0" MsgID="bf82a0fc-0f08-464f-8fed-ac8c60f3a1ce" Timestamp="Sun Aug 23 21:49:23 CDT 2020" Version="1.0"/>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<lij:Response>
<lij:Country Code="1" Continent="NA" Name="US">
<lij:State Code="713" Name="Texas" StateCode="TX"/>
<lij:State Code="678" Name="Georgia" StateCode="GA"/>
<lij:State Code="480" Name="Arizona" StateCode="AZ"/>
</lij:Country>
<lij:Demographics Capital="Washington" Currency="Dollar" Language="English"/>
</lij:Response>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
--- Using Jaxb2Marshaller Object ---
Element's Attribute: US
Sub Element List's Attribute: Georgia
Another Element's Attribute: Washington
--- Print SOAPMessage as String ---
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:lij="http://www.learninjava.com/soaptoobject/1_0/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Header>
<lijcommon:LIJHeader xmlns:lijcommon="http://www.learninjava.com/common/1_0" MsgID="bf82a0fc-0f08-464f-8fed-ac8c60f3a1ce" Timestamp="Sun Aug 23 21:49:23 CDT 2020" Version="1.0"/>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<lij:Response>
<lij:Country Code="1" Continent="NA" Name="US">
<lij:State Code="713" Name="Texas" StateCode="TX"/>
<lij:State Code="678" Name="Georgia" StateCode="GA"/>
<lij:State Code="480" Name="Arizona" StateCode="AZ"/>
</lij:Country>
<lij:Demographics Capital="Washington" Currency="Dollar" Language="English"/>
</lij:Response>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
--- Using Jaxb2Marshaller Object ---
Element's Attribute: US
Sub Element List's Attribute: Georgia
Another Element's Attribute: Washington
Remember!
1. Be careful while defining Response object. Use @XmlRootElement only once at the root element only
2. Attributes should be annotated with @XmlAttribute and Element should annotated with @XmlElement annotations
3. Use namespace prefix and namespace URIs at the right places
You can find source of the working application at the end. Check "Download Source" section.
Way 2: SOAPMessage to Java Object Using XMLStreamReader
1. Convert SOAPMessage to Java Object
loading...
2. Unmarshall using XMLStreamReader
loading...
Way 3: SOAPMessage to Java Object Using Document
1. Convert SOAPMessage to Java Object
loading...
2. Unmarshall using Document
loading...
Bonus Utilities
1. Get Document object from SOAPMessage as String
loading...
loading...
2. Get String from SOAPMessage using Stream
loading...
loading...
Thats all folks !! Happy coding. If you feel this helped you, keep supporting us by or or below or on the articles on social media.