2011-04-29 165 views
1

我需要在spring ws 2.0客戶端中將自定義soap頭添加到out out soap消息中。spring ws客戶端攔截器示例

我使用的是客戶端攔截如下.....

public class MyWebServiceClientInterceptor implements ClientInterceptor { 

    private QName qname = new QName(WEB_SERVICE_NAMESPACE, SOAP_HEADER_PART); 
    private String testText = "<userName>myUser</userName><role>everything</role>"; 

    @Override 
    public boolean handleRequest(MessageContext context) throws WebServiceClientException { 
     SoapMessage soapMessage = (SoapMessage) context.getRequest(); 
     SoapHeader soapHeader = soapMessage.getSoapHeader(); 
     SoapHeaderElement newHeader = soapHeader.addHeaderElement(qname); 
     newHeader.setActorOrRole(MY_SOAP_ACTOR); 
     newHeader.setText(testText); 
     return true; 
    } 
} 

我已確認攔截器被調用,但頭似乎並沒有得到補充。我的意思是做某種「提交」將標題應用於消息?

回答

4

不,你不需要做某種類型的提交。我做或多或少同樣的事情(雖然我叫它從WS模板的sendAndRecieve方法中:

  SoapMessage soapMessage = (SoapMessage) message; 
     SoapHeader soapHeader = soapMessage.getSoapHeader(); 
     QName qname = QNameUtils.createQName(...,...); 
     SoapHeaderElement element = soapHeader.addHeaderElement(qname); 
     element.setText(text); 

你嘗試過打開消息記錄,看看有什麼消息實際上可以看喜歡當它被髮送出去?

您可以通過創建一個log4j.properties並添加這些行做到這一點:

#log4j.logger.org.springframework.ws.client.MessageTracing.sent=TRACE 
#log4j.logger.org.springframework.ws.client.MessageTracing.received=TRACE