如何从SOAP消息中删除空标题?

时间:2015-07-22 12:26:31

标签: java soap webservice-client spring-ws soapheader

我正在使用Spring-WS来使用Webservice,如果SOAP包含有空的 header 元素,那么它就会有用。我发现默认的SOAPMessage实现添加了一个。

如何删除它?

提前致谢

1 个答案:

答案 0 :(得分:9)

http://docs.oracle.com/javaee/5/tutorial/doc/bnbhr.html

  

下一行是一个空的SOAP标头。你可以通过调用删除它   getSOAPHeader调用之后的header.detachNode。

所以这是普通SAAJ的解决方案:

        MessageFactory messageFactory = MessageFactory.newInstance("SOAP 1.2 Protocol");
        SOAPMessage message = messageFactory.createMessage();
        message.getSOAPHeader().detachNode(); // suppress empty header

以下是使用基于WebServiceMessageCallback的spring-ws this thread的解决方案:

public void marshalWithSoapActionHeader(MyObject o) {

    webServiceTemplate.marshalSendAndReceive(o, new WebServiceMessageCallback() {

        public void doWithMessage(WebServiceMessage message) {
            SaajSoapMessage saajSoapMessage = (SaajSoapMessage) message;
            SOAPMessage soapMessage = saajSoapMessage.getSaajMessage();
            SOAPPart soapPart = soapMessage.getSOAPPart();
            SOAPEnvelope envelope = soapPart.getEnvelope();
            SOAPHeader header = soapMessage.getSOAPHeader(); 
            header.detachNode();
        }
    });
}