gpt4 book ai didi

java - 如何使用 Java 客户端为 SOAP 生成特定的命名空间

转载 作者:行者123 更新时间:2023-12-02 06:06:02 24 4
gpt4 key购买 nike

我对 Java 中的 SOAP 很陌生。我想创建一个生成与此类似的信封的客户端:

<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:a="http://www.w3.org/2005/08/addressing">
<s:Header>
<a:Action s:mustUnderstand="1">http://www.w3.org/2009/02/ws-tra/Create</a:Action>
<h:ChannelIdentifier xmlns:h="http://my-web-site.org/my-trans/identifiers/1.0/" xmlns="http://my-web-site.org/my-trans/identifiers/1.0/">4</h:ChannelIdentifier>
<h:DocumentIdentifier xmlns:h="http://my-web-site.org/my-trans/identifiers/1.0/" xmlns="http://my-web-site.org/my-trans/identifiers/1.0/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" scheme="">XXXXXX</h:DocumentIdentifier>
<h:MessageIdentifier xmlns:h="http://my-web-site.org/my-trans/identifiers/1.0/" xmlns="http://my-web-site.org/my-trans/identifiers/1.0/">d8c314a3-6add-474c-871a-e0872612beeb</h:MessageIdentifier>
<a:MessageID>urn:uuid:856e3d41-92ef-4332-8a36-82d98b436fb4</a:MessageID>
<a:ReplyTo>
<a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
</a:ReplyTo>
<a:To s:mustUnderstand="1">https://XXX.com/AP1/ResourceService.svc</a:To>
</s:Header>
<s:Body xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Create xmlns="http://www.w3.org/2009/02/ws-tra">
<message xmlns="">&lt;AccessPoint.BO.Document&gt;&lt;![CDATA[Dummy Content]]&gt;&lt;/AccessPoint.BO.Document&gt;</message>
</Create>
</s:Body>
</s:Envelope>

这里的主要挑战是,如何生成这些特定的命名空间,例如 a:Actions:Header。还介绍了如何在每个属性中添加 xmlns 和 xml 属性。我可以对此进行硬编码,但是有任何标准可以做到这一点吗?

我更喜欢使用javax.xml.soap.*,但如果有更好的库,请告诉我。

最佳答案

使用此草案(这需要更多调试):

SOAPMessage soap = MessageFactory.newInstance().createMessage();

SOAPEnvelope envelope = soap.getSOAPPart().getEnvelope();
envelope.addAttribute(new QName("xmlns:s"), "http://schemas.xmlsoap.org/soap/envelope/");
envelope.addAttribute(new QName("xmlns:a"), "http://www.w3.org/2005/08/addressing");




SOAPHeader header = soap.getSOAPHeader();
header.addAttribute(new QName("xmlns:xsd"), "http://www.w3.org/2001/XMLSchema");
header.addAttribute(new QName("xmlns:xsi"), "http://www.w3.org/2001/XMLSchema-instance");

SOAPElement actionElement = header.addChildElement("a:Action");
actionElement.addAttribute(new QName("s:mustUnderstand"), "1");
actionElement.addTextNode("http://www.w3.org/2009/02/ws-tra/Create");

SOAPElement channelIdentifierElement = header.addChildElement("h:ChannelIdentifier");
channelIdentifierElement.addAttribute(new QName("xmlns:h"), "http://my-web-site.org/my-trans/identifiers/1.0/");
channelIdentifierElement.addAttribute(new QName("xmlns"), "http://my-web-site.org/my-trans/identifiers/1.0/");
channelIdentifierElement.addTextNode("4");

SOAPElement documentIdentifierElement = header.addChildElement("h:DocumentIdentifier");
documentIdentifierElement.addAttribute(new QName("xmlns:h"), "http://my-web-site.org/my-trans/identifiers/1.0/");
documentIdentifierElement.addAttribute(new QName("xmlns"), "http://my-web-site.org/my-trans/identifiers/1.0/");
documentIdentifierElement.addAttribute(new QName("xmlns:xsd"), "http://www.w3.org/2001/XMLSchema");
documentIdentifierElement.addAttribute(new QName("xmlns:xsi"), "http://www.w3.org/2001/XMLSchema-instance");
documentIdentifierElement.addAttribute(new QName("scheme"), "");
documentIdentifierElement.addTextNode("XXXXXX");

SOAPElement messageIdentifierElement = header.addChildElement("h:MessageIdentifier");
messageIdentifierElement.addAttribute(new QName("xmlns:h"), "http://my-web-site.org/my-trans/identifiers/1.0/");
messageIdentifierElement.addAttribute(new QName("xmlns"), "http://my-web-site.org/my-trans/identifiers/1.0/");
messageIdentifierElement.addTextNode("d8c314a3-6add-474c-871a-e0872612beeb");

SOAPElement messageIdElement = header.addChildElement("a:MessageID");
messageIdElement.addTextNode("urn:uuid:856e3d41-92ef-4332-8a36-82d98b436fb4");

SOAPElement replyToElement = header.addChildElement("a:ReplyTo");
SOAPElement addressElement = replyToElement.addChildElement("a:Address");
addressElement.addTextNode("http://www.w3.org/2005/08/addressing/anonymous");

SOAPElement aToElement = header.addChildElement("a:To");
aToElement.addAttribute(new QName("s:mustUnderstand"), "1");
aToElement.addTextNode("https://XXX.com/AP1/ResourceService.svc");




SOAPBody body = soap.getSOAPBody();
body.addAttribute(new QName("xmlns:xsd"), "http://www.w3.org/2001/XMLSchema");
body.addAttribute(new QName("xmlns:xsi"), "http://www.w3.org/2001/XMLSchema-instance");

SOAPElement createElement = body.addChildElement("Create");
createElement.addAttribute(new QName("xmlns"), "http://www.w3.org/2009/02/ws-tra");
SOAPElement messageElement = createElement.addChildElement("message");
messageElement.addAttribute(new QName("xmlns"), "");
messageElement.addTextNode("&lt;AccessPoint.BO.Document&gt;&lt;![CDATA[Dummy Content]]&gt;&lt;/AccessPoint.BO.Document&gt;");




ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
soap.writeTo(outputStream);
System.out.println(new String(outputStream.toByteArray()));

关于java - 如何使用 Java 客户端为 SOAP 生成特定的命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22286251/

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