简体   繁体   English

具有Web服务的应用程序中的异常处理的最佳方式

[英]The best way of exception handling in an app with web services

I have an application which consists of SOAP and REST web services and a simple HTTP access. 我有一个应用程序,它包含SOAP和REST Web服务以及简单的HTTP访问。 All of them convert incoming requests and send them to a handler. 它们都转换传入的请求并将它们发送给处理程序。 The most painful thing is exception handling. 最痛苦的是异常处理。 In order to return the right response, I have to wrap every method with try-catch block and create a response there. 为了返回正确的响应,我必须使用try-catch块包装每个方法并在那里创建响应。

I thought that I could create a filter which could do it. 我以为我可以创建一个可以做到的过滤器。 But how can the filter recognise the source of it (soap, rest frontend) so I knew that I should return a SOAP or other response? 但是过滤器如何识别它的来源(肥皂,休息前端),所以我知道我应该返回SOAP或其他响应?

It depends on the WS framework you use. 它取决于您使用的WS框架。 All I know have some sort of interceptors/aspects that you can inject and handle exceptions in one place. 我所知道的有一些拦截器/方面,你可以在一个地方注入和处理异常。 For instance in there is even a special outbound error chain where you can plug your own interceptors. 例如,在 ,甚至还有一个特殊的出站错误链 ,您可以在其中插入自己的拦截器。

Obviously try-catch in every method is a bad idea. 显然,在每种方法中try-catch都是一个坏主意。

In layer of below Web-Service Layer , you have to create your custom Exception and in Web-Service layer you have to use try-catch approach for achieve occurred exception and in catch block log and convert it to your custom web service layer exception. Web-Service Layer下面Web-Service Layer ,您必须创建自定义Exception并且在Web-Service layer您必须使用try-catch方法来实现occurred exception并在catchlog中将其转换为您的自定义Web服务层异常。 I show this approach in following: 我在下面展示了这种方法:

@WebService
public class EmployeeWS
{
    @WebMethod
    public void add(Employee em) throws CustomWebServiceException
    {
       try
       {
         // call facade layer method  
       }
       catch(Exception e)
       {
          logger.error(e.getMessage());
          throw new CustomWebServiceException(e);
       }        
    }
}

Alternative using try catch in any Web-Method ,you can use AOP approch(for sample Spring AOP ) or interceptor approach in Web-Service frameworks (for sample SOAPHandler<T> in JAX-WS ). 在任何Web-Method使用try catch的替代Web-Method ,您可以在Web-Service frameworks使用AOP approch(用于示例Spring AOP )或interceptor方法(对于JAX-WS示例SOAPHandler<T> )。

Note : In JAX-WS standard, you can't throw a RuntimeException because Exception must specify in final WSDL and if you throw a RuntimeException your web service client don't achieve your CustomException , in another your Web-Method need to throws in itself signature. 注意 :在JAX-WS标准中,您不能throw RuntimeException因为Exception必须在最终的WSDL指定,如果throw RuntimeException您的Web服务客户端不会实现您的CustomException ,而另一个Web-Method需要抛出自己的抛出签名。

You can see selected Web-Service faramework documents for more information. 您可以查看选定的Web-Service框架文档以获取更多信息。

It sounds that you are not using any framework because that was typical frameworks provide. 听起来你没有使用任何框架,因为这是典型的框架提供的。 For example Spring allows you to decouple the code from exception handling and define your custom exception handlers. 例如,Spring允许您将代码与异常处理分离,并定义自定义异常处理程序。

In your case you generally have 2 solutions. 在您的情况下,您通常有2个解决方案。

(1) You can use Decorator pattern: wrap each service with decorator where each method is implemented as (1)您可以使用Decorator模式:使用decorator包装每个服务,其中每个方法都实现为

try {
    call real method
} catch() {
   send error to client
}

Since it is very verbose you can save time using Dynamic proxy (feature that was introduced in java 5). 由于它非常冗长,您可以使用动态代理(java 5中引入的功能)节省时间。 So, you can dynamically wrap each service (if your services have defined interface). 因此,您可以动态地包装每个服务(如果您的服务已定义接口)。

(2) You can solve it using servlet API's error page: (2)您可以使用servlet API的错误页面解决它:

javax.servlet.ServletException /servlet/ErrorDisplay javax.servlet.ServletException / servlet / ErrorDisplay

for more details see http://java.sun.com/developer/technicalArticles/Servlets/servletapi2.3/ 有关详细信息,请参阅http://java.sun.com/developer/technicalArticles/Servlets/servletapi2.3/

You can customize your class!! 你可以自定义你的课程!! Do it! 做吧!

Take easy on diagnostic errors, like insert a protocol number, message Log, message client, etc... 轻松诊断错误,例如插入协议号,消息日志,消息客户端等...

http://java.globinch.com/enterprise-java/web-services/jax-ws/jax-ws-exceptions-faults-annotation-exception-and-fault-handling-examples/#Pre-Requisites http://java.globinch.com/enterprise-java/web-services/jax-ws/jax-ws-exceptions-faults-annotation-exception-and-fault-handling-examples/#Pre-Requisites

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM