1

I am trying to unmarshall below SOAP response to java POJO using JAXB in groovy but I am running into some issues

Below is my XML

String xml = '''<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soap:Body>
      <SignResult xmlns="http://www.tw.com/tsswitch">
         <Result>
            <Code>OK</Code>
            <Desc>The operation completed successfully</Desc>
         </Result>
         <SignedDocument>TUlNRS1WZXJzaW9uOiAxLjANCkRhdGU6IFdlZCwgMTkgQXByIDIwMTcgMDY6MDA6NDMgKzINCkNvbnRlbnQtVHlwZTogbXVsdGlwYXJ0L3NpZ25lZDsgcHJvdG9jb2w9ImFwcGxpY2F0aW9uL3BrY3M3LXNpZ25hdHVyZSI7IG1pY2FsZz0NCg==</SignedDocument>
         <Details>success</Details>
      </SignResult>
   </soap:Body>
</soap:Envelope>'''

Below is my SignResponse POJO

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "SignResponse", propOrder = {
    "result",
    "signedDocument",
    "archive",
    "details"
})
public class SignResponse {

    @XmlElement(name = "Result")
    protected Result result;
    @XmlElement(name = "SignedDocument")
    protected byte[] signedDocument;
    @XmlElement(name = "Archive")
    protected byte[] archive;
    @XmlElement(name = "Details")
    protected String details;
    }

Below is my first approach

javax.xml.soap.SOAPMessage message = javax.xml.soap.MessageFactory.newInstance().createMessage(null, new java.io.ByteArrayInputStream(xml.getBytes()));
javax.xml.bind.Unmarshaller unmarshaller = javax.xml.bind.JAXBContext.newInstance(SignResponse.class).createUnmarshaller();
SignResponse signResponse = unmarshaller.unmarshal(message.getSOAPBody().extractContentAsDocument()); //line 22

but its failing with below exception

Caused by: java.lang.NoSuchMethodError: org.apache.axiom.om.OMAbstractFactory.getMetaFactory(Ljava/lang/String;)Lorg/apache/axiom/om/OMMetaFactory;
at org.apache.axis2.saaj.SOAPPartImpl.<init>(SOAPPartImpl.java:141)
at org.apache.axis2.saaj.SOAPMessageImpl.<init>(SOAPMessageImpl.java:116)
at org.apache.axis2.saaj.MessageFactoryImpl.createMessage(MessageFactoryImpl.java:133)
at org.codehaus.groovy.vmplugin.v7.IndyInterface.selectMethod(IndyInterface.java:218)
at AaTestGroovyTest.run(AaTestGroovyTest:22)

Below is my second approach

import javax.xml.stream.*
import javax.xml.bind.*

Reader reader = new StringReader(xml);
XMLInputFactory factory = XMLInputFactory.newInstance(); // Or newFactory()
XMLStreamReader xsr = factory.createXMLStreamReader(reader);    
xsr.nextTag(); // Advance to Envelope tag
xsr.nextTag(); // Advance to Body tag
xsr.nextTag(); // Advance to getNumberResponse tag


JAXBContext jc = JAXBContext.newInstance(SignResponse.class);
Unmarshaller unmarshaller1 = jc.createUnmarshaller();
JAXBElement<SignResponse> je = unmarshaller1.unmarshal(xsr, SignResponse.class);
def signResponse = je.getValue();
println "signedDocument = "+signResponse.signedDocument

but am getting signedDocument = null for the second approach

can someone please help me with this?

OTUser
  • 3,788
  • 19
  • 69
  • 127

1 Answers1

1

Not sure if you really need the solution only using Jaxb in Groovy while it is very simple, one liner, to read and extract it using XmlSlurper as given below:

You should be able to get the SingedDocument value from the xml using below statement in Groovy.

//pass xml string to below parseText method
println new XmlSlurper().parseText(xml).'**'.find {it.name() == 'SignedDocument'}.text()

You can quickly try online Demo

EDIT: Update based on the OP's comment
It can be done easily:

def byteArraySignedDocument = new XmlSlurper().parseText(xml).'**'.find {it.name() == 'SignedDocument'}.text() as byte[]
assert byteArraySignedDocument instanceof byte[]
Rao
  • 20,781
  • 11
  • 57
  • 77
  • `new XmlSlurper().parseText(xml).'**'.find {it.name() == 'SignedDocument'}.text()` it works but it returns a `String`, I want `SignedDocument` as `byte[]` – OTUser Apr 19 '17 at 15:13
  • Its kinda working but the string representation of the byte[] is scrambled,Thsts the reason am trying to do it with Unmarshall here is more info about my issue http://stackoverflow.com/questions/39819445/soap-request-over-https-in-java-using-client-stub/43501638#43501638 – OTUser Apr 19 '17 at 16:54