ClientInterceptor - 捕获404场景

时间:2017-02-06 20:02:48

标签: java spring spring-boot

我正在使用 ClientInterceptor 拦截我的应用程序发出的所有外部soap调用并记录请求和响应。我能够捕获请求/响应和故障情况。但是,当我看到由于端点配置错误而导致404错误时,我无法使用此接口提供的方法捕获此错误。有没有办法使用此接口捕获404错误?如果没有,你能帮我告诉我需要探索的替代方案吗?

  

org.springframework.ws.client.support.interceptor.ClientInterceptor

1 个答案:

答案 0 :(得分:3)

org.springframework.ws.client.support.interceptor.ClientInterceptor.handleFault()仅适用于拦截传入响应错误的过程。这是规范定义的SOAP错误。因此SOAP 1.11.2规范定义了在SOAP出现故障时返回的状态代码(例如,在请求消息信封中出现格式错误的XML)。 SOAP 1.1为故障指定了500状态代码,SOAP 1.2为发送方故障指定了400状态代码,为所有其他故障指定了500状态代码。如果出现故障,服务器将在包含SOAP Fault元素的响应中返回SOAP消息,然后可以将该元素传递给ClientInterceptor.handleFault()方法。但404状态代码表明它不是SOAP故障而是系统错误。因此,如果您还需要处理该情况,则需要覆盖WebServiceTemplate.handleError()方法。

这样就可以了解

public class CustomWebServiceTemplate extends WebServiceTemplate{

    @Override
    protected Object handleError(WebServiceConnection connection, WebServiceMessage request) throws IOException {
        log.info("Log the error case in here");
        return super.handleError(connection, request);
    }
}