2015-06-17 23 views
0

我遇到的問題是Apache CXF將名稱空間放在元素中而不是soap信封中。Apache CXF客戶端命名空間位於元素內而不是信封

我使用codegen maven插件從WSDL生成客戶端代碼。

這裏是WSDL中的命名空間:

​​

當我火了SOAP請求,我可以在日誌中看到下面的消息已發送:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
<soap:Body> 
    <ns1:userRegistrationRequest xmlns:ns1="http://some.com/namespace1"> 
     <InputParameter xmlns:ns2="http://some.com/namespace1"> 
      <ns2:Message> 
       <ns2:Value xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
       <ns2:HashTable> 
        <ns2:Item key="action"> 
         <ns2:Value>some_action</ns2:Value> 
        </ns2:Item> 
       </ns2:HashTable> 
      </ns2:Message> 
     </InputParameter> 
    </ns1:userRegistrationRequest> 
</soap:Body> 

的問題是當我構造消息時,我的代碼中的Value元素爲null,但在這裏它與XSI命名空間一起出現。

我所期待得到的是這樣的事情(Value元素不應該存在和XSI應在信封元素):

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
<soap:Body> 
    <ns1:userRegistrationRequest xmlns:ns1="http://some.com/namespace1"> 
     <InputParameter xmlns:ns2="http://some.com/namespace1"> 
      <ns2:Message> 
       <ns2:HashTable> 
        <ns2:Item key="action"> 
         <ns2:Value>some_action</ns2:Value> 
        </ns2:Item> 
       </ns2:HashTable> 
      </ns2:Message> 
     </InputParameter> 
    </ns1:userRegistrationRequest> 
</soap:Body> 

有沒有人有一個想法如何預防CXF生成空的Value元素並將該名稱空間放在soap envelope元素中?

回答

0

好的,這樣回答我的問題。這個問題有兩個方面。

首先,將名稱空間添加到信封元素。它可以通過寫一個自定義攔截器並在一些早期階段註冊完成。是這樣的:

import java.util.Map; 

import org.apache.cxf.binding.soap.SoapMessage; 
import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor; 
import org.apache.cxf.interceptor.Fault; 
import org.apache.cxf.phase.Phase; 

public class MyInterceptor extends AbstractSoapInterceptor { 

    private Map<String, String> nsMap; 

    public MyInterceptor(Map<String, String> nsMap) { 
     super(Phase.USER_LOGICAL); 
     this.nsMap = nsMap; 
     } 

    public void handleMessage(SoapMessage message) throws Fault { 
     message.put("soap.env.ns.map", nsMap); 
     message.put("disable.outputstream.optimization", Boolean.TRUE); 
    } 

} 

我發現了上述溶液here

其次,以除去空值元件一個xsd應改爲該元件具有的minOccurs =「0」和的nillable =「假」。