gpt4 book ai didi

java - 在 groovy 中使用 JAXB 解码对 java POJO 的 SOAP 响应

转载 作者:行者123 更新时间:2023-11-30 06:47:16 27 4
gpt4 key购买 nike

我正在尝试使用 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

有人可以帮我解决这个问题吗?

最佳答案

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

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

//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[]

关于java - 在 groovy 中使用 JAXB 解码对 java POJO 的 SOAP 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43486483/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com