将名称空间声明从有效负载移动到轴创建的Web服务上的信封

Ric*_*mon 4 axis web-services

我刚刚创建了一个使用axis和eclipse的Web服务客户端,它不能与我的Web服务提供者一起使用.Web服务客户端创建的消息如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope 
  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
  xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soapenv:Body>
    <enviarMensajeRequest 
       xmlns="http://www.springframework.org/spring-ws/Imk-Zenkiu-Services">
      <usuario>someuser</usuario>
      <clave>somepassword</clave>
      <mensaje>somemessage</mensaje>
      <contacto>
        <buzonSMS>somenumber</buzonSMS>
        <primerNombre>somefirstname</primerNombre>
        <primerApellido>somelastname</primerApellido>
      </contacto>
    </enviarMensajeRequest>
  </soapenv:Body>
</soapenv:Envelope>
Run Code Online (Sandbox Code Playgroud)

我发现该消息没有错,但我的提供者坚持认为该消息应该是:

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope 
  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
  xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:imk="http://www.springframework.org/spring-ws/Imk-Zenkiu-Services">
  <soapenv:Body>
     <imk:enviarMensajeRequest>
        <imk:usuario>someuser</imk:usuario>
        <imk:clave>somepassword</imk:clave>
        <imk:mensaje>somemessage</imk:mensaje>
        <imk:contacto>
           <imk:buzonSMS>somenumber</imk:buzonSMS>
           <imk:primerNombre>somefirstname</imk:primerNombre>
           <imk:primerApellido>somelastname</imk:primerApellido>
        </imk:contacto>
     </imk:enviarMensajeRequest>
  </soapenv:Body>
</soapenv:Envelope>
Run Code Online (Sandbox Code Playgroud)

注意命名空间声明从参数移动enviarMensajeRequest到参数soapenv:Envelope和限定条件imk:.我在这个过程中尝试了很多组合,但我的web服务,wsdl和xml知识非常有限.提供者说他们无法告诉我这件事.有任何想法吗?也许我可以使用不同的框架来创建正确的客户端.

Jus*_*ick 8

您的提供程序错误,消息在语义上是等效的; 你的不合格,他们是合格的.您使用的是Axis还是Axis2?如果您正在使用Axis,我建议您切换到Axis2以获得更强大,符合标准的SOAP堆栈(两种产品都很糟糕,但Axis2不那么糟糕).

我假设你用wsdl2java创建你的客户端?如果您无法使用此工具以您喜欢的方式生成消息,那么您最好的选择是以编程方式生成消息.使用Axis2,您可以使用AXIOM API执行此操作.有关示例API用法,请参阅此链接.请注意,对于大多数方法,例如createOMElement,您可以选择传递名称空间前缀.因此,如果您的提供者需要它,那么您可以传递包含"imk"的String作为namespacePrefix参数.


如果您最终以编程方式执行此操作并且您将只编写一个简单的客户端,那么我强烈建议您放弃Axis/Axis2方法并改为使用JAX-WS堆栈,因为它是1.6的Java的一部分.API更清晰,文档更好.例如,以下是我编写的一个非常简单的客户端,用于向我们的JIRA服务器发送SOAP请求.示例代码创建限定和非限定元素.

QName port = new QName(endpoint, "subversionsoapservice-v2");
QName serviceName = new QName(endpoint, "ISubversionSoapServiceService");

Service service = Service.create(serviceName);
service.addPort(port, SOAPBinding.SOAP11HTTP_BINDING, endpoint);

Dispatch<SOAPMessage> dispatch = service.createDispatch(port, SOAPMessage.class, Service.Mode.MESSAGE);
MessageFactory factory = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
SOAPMessage request = factory.createMessage();
SOAPBody body = request.getSOAPBody();

SOAPElement reindexRepository = body.addChildElement("reindexRepository", "jira", "http://soap.ext.plugin.jira.atlassian.com");
SOAPElement in0 = reindexRepository.addChildElement("in0");
in0.addTextNode("test");

request.saveChanges();
dispatch.invoke(request);
Run Code Online (Sandbox Code Playgroud)

客户端发送的XML如下所示:

<?xml version="1.0"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Body>
        <jira:reindexRepository xmlns:jira="http://soap.ext.plugin.jira.atlassian.com">
            <in0>test</in0>
        </jira:reindexRepository>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Run Code Online (Sandbox Code Playgroud)

  • 这很棒,谢谢.对于其他任何人,您可以在soap标头中设置名称空间声明,如下所示:`SOAPMessage request = factory.createMessage(); SOAPEnvelope envelope = request.getSOAPPart().getEnvelope(); envelope.addNamespaceDeclaration("uri","uri:foo.bar.com"); request.saveChanges();`然后创建带有名称空间前缀的元素,如下所示:`SOAPBody body = request.getSOAPBody(); SOAPElement ping = body.addChildElement("foo","uri");` (3认同)