在groovy中使用JAXB取消对Java POJO的SOAP响应

时间:2017-04-19 04:36:58

标签: java web-services soap groovy jaxb

我试图在groovy中使用JAXB对Java POJO的SOAP响应进行解组,但是我遇到了一些问题

下面是我的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>'''

以下是我的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;
    }

以下是我的第一个方法

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

但是它失败了以下异常

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)

以下是我的第二种方法

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

但第二种方法正在获得signedDocument = null

有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:1)

不确定您是否真的只需要在Groovy中使用Jaxb的解决方案,而非常简单,一个内容,使用XmlSlurper读取并提取它,如下所示:

您应该可以使用SingedDocument中的以下语句从xml获取Groovy值。

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

您可以快速在线试用 Demo

编辑:根据OP的评论进行更新
它可以很容易地完成:

def byteArraySignedDocument = new XmlSlurper().parseText(xml).'**'.find {it.name() == 'SignedDocument'}.text() as byte[]
assert byteArraySignedDocument instanceof byte[]