RESTEasy 中的 ExceptionMapper 问题

发布于 2024-09-07 18:27:57 字数 652 浏览 1 评论 0原文

在我的 JAXRS 应用程序中,我有 2 个 ExceptionMapper 注册为 @Provider

public class MyExceptionMapper implements ExceptionMapper<Exception> {...}

public class MyCustomRuntimeExceptionMapper implements ExceptionMapper<MyCustomRuntimeException> {...}

当我的应用程序抛出新的 MyCustomRuntimeException 时,异常会在 >MyExceptionMapper,尽管(JAX-RS 规范规定)它应该在 MyCustomRuntimeExceptionMapper 中捕获。

JAXRS 是这样说的 -

JAX-RS 也支持异常继承。当出现异常时 抛出,JAX-RS 将首先尝试找到一个异常映射器 异常的类型。如果找不到,它将寻找映射器 可以处理异常的超类。还会继续这样的 处理直到没有更多的超类可以匹配。

有谁知道这里发生了什么事吗?

In my JAXRS application, I have 2 ExceptionMapper registered as @Provider

public class MyExceptionMapper implements ExceptionMapper<Exception> {...}

public class MyCustomRuntimeExceptionMapper implements ExceptionMapper<MyCustomRuntimeException> {...}

When my application throws a new MyCustomRuntimeException, the exception is caught inside the MyExceptionMapper, even though (JAX-RS spec says) it should be caught inside MyCustomRuntimeExceptionMapper.

Here's what JAXRS says -

JAX-RS supports exception inheritance as well. When an exception is
thrown, JAX-RS will first try and find an ExceptionMapper for that
exception’s type. If it cannot find one, it will look for a mapper
that can handle the exception’s superclass. It will continue this
process until there are no more superclasses to match against.

Does anyone have a clue, whats going on here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黎夕旧梦 2024-09-14 18:27:57

我可以通过在我的 web.xml 中声明如下提供者来正确实现:

<context-param>
        <param-name>resteasy.providers</param-name>
        <param-value>CustomExceptionHandler,DefaultExceptionHandler</param-value>
</context-param>

I am able to get it right, by declaring providers as below, in my web.xml:

<context-param>
        <param-name>resteasy.providers</param-name>
        <param-value>CustomExceptionHandler,DefaultExceptionHandler</param-value>
</context-param>
薄荷梦 2024-09-14 18:27:57

我也有这个问题。看起来好像它在查找子类之前先找到了异常的超类。这使我无法使用 ExceptionMapper 来处理异常。我必须处理一个具有子类的特定于应用程序的异常;我不能为它们提供单独的异常映射器。我有一个超类的异常映射器,然后是一堆“if (e instanceof SubClassA)”语句。

I had a problem with this too. It appears as if it finds the superclass of the exception before looking for the subclass. This keeps me from having an ExceptionMapper for Exception. There's an application-specific exception I have to deal with that has subclasses; I can't have separate exception mappers for them. I have one exception mapper for the superclass and then a bunch of "if (e instanceof SubClassA)" statements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文