如果 SOAP 信封具有空的 header 元素,我正在使用 Spring-WS 来使用 Webservice。我发现默认的 SOAPMessage 实现增加了一个。

我怎样才能删除它?

提前致谢

最佳答案

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



所以这里是普通 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();
        }
    });
}

10-08 12:57