【问题标题】:How do I add a namespace reference to a SOAP response with Apache Axis2 and WSDL2Java如何使用 Apache Axis2 和 WSDL2Java 向 SOAP 响应添加命名空间引用
【发布时间】:2010-09-09 15:38:24
【问题描述】:

我正在查看我正在开发的 Web 服务的 SOAP 输出,我注意到一些奇怪的事情:

<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
   <soapenv:Body>
      <ns1:CreateEntityTypesResponse xmlns:ns1="http://somedomain.com/wsinterface">
         <newKeys>
            <value>1234</value>
         </newKeys>
         <newKeys>
            <value>2345</value>
         </newKeys>
         <newKeys>
            <value>3456</value>
         </newKeys>
         <newKeys xsi:nil="1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
         <newKeys xsi:nil="1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
         <errors>Error1</errors>
         <errors>Error2</errors>
      </ns1:CreateEntityTypesResponse>
   </soapenv:Body>
</soapenv:Envelope>

我有两个 nil 的 newKeys 元素,两个元素都插入了 xsi 的命名空间引用。我想将该命名空间包含在 soapenv:Envelope 元素中,以便命名空间引用只发送一次。

我正在使用 WSDL2Java 生成服务框架,因此我无法直接访问 Axis2 API。

【问题讨论】:

    标签: java soap namespaces axis2 wsdl2java


    【解决方案1】:

    使用 WSDL2Java

    如果您使用过 Axis2 WSDL2Java 工具,那么您会被它为您生成的东西所困扰。但是,您可以尝试在本节中更改骨架:

       // create SOAP envelope with that payload
       org.apache.axiom.soap.SOAPEnvelope env = null;
       env = toEnvelope(
           getFactory(_operationClient.getOptions().getSoapVersionURI()),
           methodName,
           optimizeContent(new javax.xml.namespace.QName
           ("http://tempuri.org/","methodName")));
    
    //adding SOAP soap_headers
    _serviceClient.addHeadersToEnvelope(env);
    

    要将命名空间添加到信封中,请在其中某处添加以下行:

    OMNamespace xsi = getFactory(_operationClient.getOptions().getSoapVersionURI()).
        createOMNamespace("http://www.w3.org/2001/XMLSchema-instance", "xsi");
    
    env.declareNamespace(xsi);
    

    手工编码

    如果您正在“手动编码”服务,您可能会执行以下操作:

    SOAPFactory fac = OMAbstractFactory.getSOAP11Factory();   
    SOAPEnvelope envelope = fac.getDefaultEnvelope();
    OMNamespace xsi = fac.createOMNamespace("http://www.w3.org/2001/XMLSchema-instance", "xsi");
    
    envelope.declareNamespace(xsi);
    OMNamespace methodNs = fac.createOMNamespace("http://somedomain.com/wsinterface", "ns1");
    
    OMElement method = fac.createOMElement("CreateEntityTypesResponse", methodNs);
    
    //add the newkeys and errors as OMElements here...
    

    在 aar 中暴露服务

    如果您在 aar 内创建服务,您可能能够影响使用目标命名空间或模式命名空间属性生成的 SOAP 消息(请参阅this article)。

    希望对您有所帮助。

    【讨论】:

      【解决方案2】:

      其他选项是变量 MY_QNAME 的前缀为空。

      public static final QName MY_QNAME = new QName("http://www.hello.com/Service/",
                  "tagname",
                  "prefix");
      

      所以,如果你填写它,那么它就会起作用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多