gpt4 book ai didi

java - 基于 jax-rs 的框架应该使用单个异常映射器吗?

转载 作者:行者123 更新时间:2023-11-30 10:26:56 27 4
gpt4 key购买 nike

我们有一个大型遗留应用程序,我们最近向其添加了基于 jersey/jax-rs 的功能。我们的想法是,我们将为我们的 UI 层实现新功能作为 restful API。第一个原型(prototype)运行良好 - 但它缺乏适当的错误处理。

我仔细查看了初始实现,发现有一个“基本任务”类注册了一个特定的 ExceptionMapper。 .这个想法是,将有不同的分离的子任务扩展该基本任务。

我最初的想法是:每个子任务 都应该有自己的特定异常映射器。

换句话说:我看到两个不同的选项:

  • 一个中央异常映射器,处理任何我们将随时间实现的众多子任务中抛出的所有异常
  • 一个映射器每个子任务

选项 1 导致好,一段核心代码 - 但与选项 2 相比灵 active 要差得多(它开始于这样的事情:通常我们的子任务在写入日志时有自己的“日志 ID” - 但中央映射器没有'知道它的上下文并且必须一直使用完全相同的日志 ID)。

我的问题是:硬技术事实(或公认的“最佳程序”)是否可以在两个选项之间进行选择?

最佳答案

在我看来,一个通用的异常处理程序可以捕获所有内容 public class GenericExceptionMapperProvider implements ExceptionMapper<Throwable> ,并且,您可以根据需要继续添加更多异常处理程序。

JAX-RS supports exception inheritance as well. When an exception is thrown, JAX-RS will first try to 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. read more

这是我写的帖子:http://blog.nafiux.com/posts/jax-rs-exception-handling/

关于java - 基于 jax-rs 的框架应该使用单个异常映射器吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45507434/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com