我似乎找不到解决这个问题的合适方法。我想根据发生的情况向用户处理和显示不同的消息。我正在使用Spring WS并调用marshalSendAndReceive
。目前在返回服务方法时,我会简单地catch (WebServiceIOException e)
,问题是当连接被拒绝(错误凭据)和查询时间超过客户端指定的时间限制(超时)时,都会抛出此异常?有没有适当的方式来处理这两个问题,而不提供一个通用的“出了些问题”的消息。
我似乎找不到解决这个问题的合适方法。我想根据发生的情况向用户处理和显示不同的消息。我正在使用Spring WS并调用marshalSendAndReceive
。目前在返回服务方法时,我会简单地catch (WebServiceIOException e)
,问题是当连接被拒绝(错误凭据)和查询时间超过客户端指定的时间限制(超时)时,都会抛出此异常?有没有适当的方式来处理这两个问题,而不提供一个通用的“出了些问题”的消息。
WebServiceTemplate
在 doSendAndReceive
方法中完成其重要工作,其中有 try...catch
块和全面的错误处理。其中之一是 triggerAfterCompletion(int interceptorIndex, MessageContext messageContext, Exception ex)
,它将委托给注入的 ClientInterceptor
。afterCompletion(MessageContext messageContext, Exception ex)
来抛出适当的业务异常即可。ClientInterception
类。Exception
类:try {
webServiceTemplate.marshalSendAndReceive(request);
} catch(Exception e){
log.error("your custom message");
}
WebServiceIOException
中提取cause
并针对每种情况执行逻辑。这是因为WebServiceTemplate
会执行以下操作:throw new WebServiceIOException("I/O error: " + ex.getMessage(), ex);
- Artem Bilan