2015-12-21 84 views
0

根據我的任務,我必須調用SOAP服務。所以,我使用xjc從wsdl生成了java類。但是我有一個調用SOAP服務的問題。我的應用程序生成這個請求:jaxb將元素名稱空間添加到元素

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
<SOAP-ENV:Header xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"/><soap:Body> 
<typ:SendMessage xmlns:ns4="http://test.user.kz/UserInfo" xmlns:q1="http://test.user.kz/CustomerInfo" xmlns:typ="http://test.user.kz/MyChannel/v1/Types"> 
    <request> 
     <requestInfo> 
      <messageId>26e96b11-8f82-421e-829a</messageId> 
     </requestInfo> 
     <requestData> 
      <data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="q1:PaymentPackageRequestType"> 
       <q1:methodName>testMethod</q1:methodName> 
      </data> 
     </requestData> 
    </request> 
</typ:SendMessage></soap:Body></soap:Envelope> 

但我需要在我的SOAP請求,我需要的數據標記指定的命名空間,LIK這樣:

 <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
<SOAP-ENV:Header xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"/><soap:Body> 
<typ:SendMessage xmlns:ns4="http://test.user.kz/UserInfo" xmlns:q1="http://test.user.kz/CustomerInfo" xmlns:typ="http://test.user.kz/MyChannel/v1/Types"> 
    <request> 
     <requestInfo> 
      <messageId>26e96b11-8f82-421e-829a</messageId> 
     </requestInfo> 
     <requestData> 
         <data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="q1:PaymentPackageRequestType" **xmlns:q1="http://payments.bee.kz/PaymentPackage"**> 
       <q1:methodName>testMethod</q1:methodName> 
      </data> 
     </requestData> 
    </request> 
</typ:SendMessage></soap:Body></soap:Envelope> 

否則目標SOAP服務給我的錯誤:

XML namespace prefix 'q1' is not defined. 

如何在數據標籤中指定名稱空間? 這是我目前的包信息:

@javax.xml.bind.annotation.XmlSchema(
     xmlns = { 
      @javax.xml.bind.annotation.XmlNs(prefix = "typ", 
        namespaceURI = "http://test.user.kz/MyChannel/v1/Types"), 
      @javax.xml.bind.annotation.XmlNs(prefix = "q1", 
        namespaceURI = "http://test.user.kz/CustomerInfo") 
     }, 
    elementFormDefault = javax.xml.bind.annotation.XmlNsForm.UNQUALIFIED, 
    attributeFormDefault = javax.xml.bind.annotation.XmlNsForm.UNSET 
) 
package kz.uni.gen; 

,所以我不能添加命名空間的xmlns:Q1 =「http://test.user.kz/CustomerInfo」數據標籤的SOAP請求。如何添加此名稱空間聲明或從SendMessage標記中移動名稱空間聲明?

回答

0

所以使用JAXB是不可能的。因此我手動在所需元素中添加命名空間。這是完整的片段:

Document document = null; 
     try { 
      document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); 
      Marshaller marshaller = JAXBContext.newInstance(SendMessage.class).createMarshaller(); 
      marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); 
      marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); 
      marshaller.setProperty("com.sun.xml.bind.characterEscapeHandler", new CharacterEscapeHandler() { 
       @Override 
       public void escape(char[] buf, int start, int len, boolean b, Writer out) throws IOException { 
        out.write(buf, start, len); 
       } 
      }); 
      QName name = new QName(NAMESPACE_URI, SendMessage.class.getSimpleName()); 
      JAXBElement<SendMessage> root = new JAXBElement<SendMessage>(name, SendMessage.class, from); 
      StringWriter writer1 = new StringWriter(); 
      marshaller.marshal(root, writer1); 
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
      dbf.setNamespaceAware(true); 
      Element node = dbf 
        .newDocumentBuilder() 
        .parse(new ByteArrayInputStream(writer1.toString().getBytes(StandardCharsets.ISO_8859_1))) 
        .getDocumentElement(); 
      Attr attr1 = document.createAttribute("xmlns:q1"); 
      attr1.setValue("http://test.user.kz/CustomerInfo"); 
      node.getElementsByTagName("data").item(0).getAttributes().setNamedItem(node.getOwnerDocument().importNode(attr1, true)); 
      return node; 
     } catch (Exception e) { 
      throw new Exception("Unable to transform POJO to XML SOAP message ", e); 

     }