【问题标题】:Spring WS: Handle connection refused and client timeout exceptionsSpring WS:处理连接被拒绝和客户端超时异常
【发布时间】:2015-01-12 08:02:34
【问题描述】:

我似乎找不到合适的解决方案。我想根据发生的情况处理并向用户显示不同的消息。我正在使用 Spring WS 并调用 marshalSendAndReceive。在返回服务方法上,我会简单地catch (WebServiceIOException e),问题是连接被拒绝(错误的凭据)和查询花费的时间超过客户端指定的时间(超时)时都会引发此异常?是否有任何适当的方法来处理这两个而不提供“出现问题”的一般消息。

【问题讨论】:

    标签: java spring web-services


    【解决方案1】:

    WebServiceTemplatedoSendAndReceive 方法中努力工作,其中有try...catch 块和全面的错误处理。其中之一是triggerAfterCompletion(int interceptorIndex, MessageContext messageContext, Exception ex),它代表注入的ClientInterceptor

    因此,您只需正确实现afterCompletion(MessageContext messageContext, Exception ex) 即可抛出相应的业务异常。

    【讨论】:

    • 恐怕我当前项目中的 Spring WS 库有点老,不提供这种行为。
    • 好吧,否则你应该从WebServiceIOException 中提取cause 并针对每种情况进行逻辑处理。那是因为WebServiceTemplate 这样做:throw new WebServiceIOException("I/O error: " + ex.getMessage(), ex);
    【解决方案2】:

    一种方法是实现ClientInterception

    另一种方法是在您向网络服务发送请求的位置捕获一般 Exception 类:

    try {
      webServiceTemplate.marshalSendAndReceive(request);
    } catch(Exception e){
       log.error("your custom message");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-15
      • 2021-10-16
      • 1970-01-01
      相关资源
      最近更新 更多