在Spring Web Service Interceptor中捕获异常并返回Soap响应

时间:2013-03-20 23:44:17

标签: spring exception soap exception-handling spring-ws

在Spring Web Service中捕获异常的最佳方法是什么,提取它的详细信息,并将其格式化为soap响应?我的错误消息详细信息必须放在Soap响应的标题中。

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ims="http://www.imsglobal.org/services/lis/cmsv1p0/wsdl11/sync/imscms_v1p">
   <soapenv:Header>
      <imsx_syncResponseHeaderInfo xmlns="http://www.imsglobal.org/services/lis/cmsv1p0/wsdl11/sync/imscms_v1p0">
         <imsx_version>V1.0</imsx_version>
         <imsx_messageIdentifier>4</imsx_messageIdentifier>
         <imsx_statusInfo>
            <imsx_codeMajor>failure</imsx_codeMajor>
            <imsx_severity>status</imsx_severity>
            <imsx_codeMinor>
               <imsx_codeMinorField>
                  <imsx_codeMinorFieldName>TargetEndSystem</imsx_codeMinorFieldName>
                  <imsx_codeMinorFieldValue>incompletedata</imsx_codeMinorFieldValue>
               </imsx_codeMinorField>
            </imsx_codeMinor>
         </imsx_statusInfo>
      </imsx_syncResponseHeaderInfo>
   </soapenv:Header>
   <soapenv:Body/>
</soapenv:Envelope>

2 个答案:

答案 0 :(得分:2)

我知道这是否是最佳方法,但我添加了 SimpleSoapExceptionResolver 对象:

import java.util.Date;
import java.util.Locale;
import org.apache.log4j.Logger;
import org.springframework.ws.WebServiceMessage;
import org.springframework.ws.context.MessageContext;
import org.springframework.ws.soap.SoapBody;
import org.springframework.ws.soap.SoapFault;
import org.springframework.ws.soap.SoapMessage;
import org.springframework.ws.soap.server.endpoint.SimpleSoapExceptionResolver;

public final class MySimpleSoapExceptionResolver 
extends SimpleSoapExceptionResolver {

    public MySimpleSoapExceptionResolver () {
        super.setOrder(HIGHEST_PRECEDENCE);     
    }

    @Override
    protected void customizeFault(  final MessageContext messageContext_, 
                                    final Object endpoint_, 
                                    final Exception exception_, 
                                    SoapFault soapFault_) {

        WebServiceMessage _webServiceMessageResponse = 
                                messageContext_.getResponse();
        SoapMessage _soapMessage = (SoapMessage) _webServiceMessageResponse;
        SoapBody _soapBody = _soapMessage.getSoapBody();

        String _message = "your error message";

        Logger _logger = Logger.getLogger(MySimpleSoapExceptionResolver.class);
        _logger.error(_message, exception_);
        soapFault_ = 
        _soapBody.addServerOrReceiverFault(_message, Locale.ENGLISH);


    }

}

答案 1 :(得分:0)

您可以实现org.springframework.ws.server.endpoint.interceptor.EndpointInterceptorAdapter类型的拦截器。在webservice配置中注册拦截器。

实现方法handleResponse(MessageContext messageContext,Object endpoint),如下所示 -

handleResponse(MessageContext messageContext, Object endpoint) {
   SoapMessage msg = (SoapMessage) messageContext.getResponse();
   SoapHeader header = msg.getSoapHeader();
   // do what you want to do with header.
}

我没有实现这个,但在CXF中使用拦截器做了类似的事情。