如何从Spring Web服务中删除Soap消息中的body元素

时间:2013-03-20 17:41:40

标签: java spring soap spring-ws

我有以下拦截器修改soap响应的标头。我还想清除肥皂响应的主体,但我无法找到任何关于这样做的文件或解决方法。建议?

  public class LisResponseInterceptor extends EndpointInterceptorAdapter {

        @Override
        public boolean handleResponse(MessageContext messageContext, Object endpoint) {
            return true;
        }

        @Override
        public boolean handleRequest(MessageContext messageContext, Object endpoint) {
            return true;
        }

        @Override
        public boolean handleFault(MessageContext messageContext, Object endpoint) {

            SoapMessage response = (SoapMessage) messageContext.getResponse();
            SoapHeader header = response.getSoapHeader();
            header.addHeaderElement(new QName("ims","imsx_syncRequestHeaderInfo"));

            SoapBody body = response.getSoapBody();

                    // clear body

            return true;
        }
    }

1 个答案:

答案 0 :(得分:1)

修正了它

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Header>
      <imsx_syncRequestHeaderInfo xmlns="ims"/>
   </SOAP-ENV:Header>
   <SOAP-ENV:Body/>
</SOAP-ENV:Envelope>


public class LisResponseInterceptor extends EndpointInterceptorAdapter {

    @Override
    public boolean handleResponse(MessageContext messageContext, Object endpoint) {
        return true;
    }

    @Override
    public boolean handleRequest(MessageContext messageContext, Object endpoint) {
        return true;
    }

    @Override
    public boolean handleFault(MessageContext messageContext, Object endpoint) {

        SoapMessage response = (SoapMessage) messageContext.getResponse();
        SoapHeader header = response.getSoapHeader();
        header.addHeaderElement(new QName("ims","imsx_syncRequestHeaderInfo"));

        SoapBody body = response.getSoapBody();

        Transformer identityTransform = null;

    try {
        identityTransform = TransformerFactory.newInstance().newTransformer();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerFactoryConfigurationError e) {
        e.printStackTrace();
    }

    try {
        identityTransform.transform(new DOMSource(null), body.getPayloadResult());
    } catch (TransformerException e) {
        e.printStackTrace();
    }

        return true;
    }
}