简体   繁体   English

如何更改使用 SOAPMessage 创建的 SOAP Web 服务请求中的命名空间前缀?

[英]How to change namespace prefix in SOAP webservice request created with SOAPMessage?

When I create my SOAP request using the javax.xml.soap.SOAPMessage class, I get a namespace prefix in my XML like <SOAP-ENV:Envelope> . When I create my SOAP request using the javax.xml.soap.SOAPMessage class, I get a namespace prefix in my XML like <SOAP-ENV:Envelope> .

Is there a simple way to change the namespace prefix to <soapenv:Envelope> ?有没有一种简单的方法可以将命名空间前缀更改为<soapenv:Envelope>

The XML namespace prefix should not matter as long as you are calling a well behaved web service.只要您调用行为良好的 web 服务,XML 命名空间前缀就无关紧要 So SOAP-ENV: , soapenv: , or whatever: are basically the same thing as long as they both refer to the same namespace.因此SOAP-ENV:soapenv:whatever:只要它们都引用相同的命名空间,它们基本上是相同的。

With that being said, if you really need to do this for some reason, here is some minimally working code.话虽如此,如果您出于某种原因确实需要这样做,这里有一些最低限度的工作代码。 I'm calling this service .我打电话给这个服务

package soap;

import java.io.IOException;
import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;

public class Client {
    public static void main(String[] args) throws SOAPException, IOException {
        MessageFactory factory = MessageFactory.newInstance();
        SOAPMessage message = factory.createMessage();

        MimeHeaders mimeHeader = message.getMimeHeaders();
        mimeHeader.setHeader("SOAPAction", "http://tempuri.org/Add");
        
        SOAPBody body = message.getSOAPBody();
        SOAPHeader header = message.getSOAPHeader();
        
        QName bodyName = new QName("http://tempuri.org/", "Add", "tmp");
        SOAPBodyElement bodyElement = body.addBodyElement(bodyName);
        
        
        SOAPElement intA = bodyElement.addChildElement(new QName("http://tempuri.org/", "intA", "tmp"));
        intA.addTextNode("123");
        
        SOAPElement intB = bodyElement.addChildElement(new QName("http://tempuri.org/", "intB", "tmp"));
        intB.addTextNode("456");

        message.writeTo(System.out);
        System.out.println();
        
        // -----------------> Now changing the prefix before making the call
        
        SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
        envelope.removeNamespaceDeclaration(envelope.getPrefix());
        
        String prefix = "soapenv";
        envelope.addNamespaceDeclaration(prefix, "http://schemas.xmlsoap.org/soap/envelope/");
        envelope.setPrefix(prefix);
        header.setPrefix(prefix);
        body.setPrefix(prefix);
        
        // <---------------- done changing the prefix
        
        message.writeTo(System.out);
        System.out.println();
        
        SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
        SOAPConnection connection = soapConnectionFactory.createConnection();
        URL endpoint = new URL("http://www.dneonline.com/calculator.asmx");
        SOAPMessage response = connection.call(message, endpoint);
        connection.close();
        
        response.writeTo(System.out);
    }
}

I've printed the request message before and after the transformation.我已经在转换前后打印了请求消息。 The output is as follows (formatting not included:D): output如下(不包括格式:D):

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
  <SOAP-ENV:Header/>
  <SOAP-ENV:Body>
    <tmp:Add xmlns:tmp="http://tempuri.org/">
      <tmp:intA>123</tmp:intA>
      <tmp:intB>456</tmp:intB>
    </tmp:Add>
  </SOAP-ENV:Body>
 </SOAP-ENV:Envelope>

 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header/>
   <soapenv:Body>
     <tmp:Add xmlns:tmp="http://tempuri.org/">
       <tmp:intA>123</tmp:intA>
       <tmp:intB>456</tmp:intB>
     </tmp:Add>
   </soapenv:Body>
 </soapenv:Envelope>

 <?xml version="1.0" encoding="utf-8"?>
 <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
     <AddResponse xmlns="http://tempuri.org/">
       <AddResult>579</AddResult>
     </AddResponse>
   </soap:Body>
</soap:Envelope>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM