1

在 Spring Web 服务中捕获异常、提取其详细信息并将其格式化为肥皂响应的最佳方法是什么?我的错误消息详细信息必须放在 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>
4

2 回答 2

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);


    }

}
于 2013-03-21T12:22:46.297 回答
0

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

像这样实现方法 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 中使用拦截器做了类似的事情。

于 2013-03-21T04:35:17.033 回答