How to remove empty header from SOAP message?

…衆ロ難τιáo~ 提交于 2019-12-07 03:32:36

问题


I am using Spring-WS for consuming Webservice which compains if SOAP envelop has empty header element. I figured out that default SOAPMessage implementation adds one.

How can I remove it?

Thanks in advance


回答1:


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

The next line is an empty SOAP header. You could remove it by calling header.detachNode after the getSOAPHeader call.

So here is the solution in plain SAAJ:

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

And here is the solution using spring-ws WebServiceMessageCallback based on 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();
        }
    });
}


来源:https://stackoverflow.com/questions/31563123/how-to-remove-empty-header-from-soap-message

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!