简体   繁体   English

使线程上的对象可用于Dropwizard异常映射器

[英]Make an object on thread available to Dropwizard Exception Mapper

We're using Dropwizard as a platform for our REST service and make use of its exception mappers to deal with any exceptions thrown during the user journey. 我们将Dropwizard用作REST服务的平台,并利用其异常映射器来处理用户使用过程中引发的所有异常。 We're using google guice for dependency injection. 我们正在使用谷歌guice进行依赖注入。

In one of our use cases, when an exception is thrown, we need to attach some extra information to the response that is not available at the point where exception is thrown. 在我们的一种使用案例中,当引发异常时,我们需要将一些额外的信息附加到在引发异常时不可用的响应。

The following flow diagram highlights the use case. 以下流程图突出了用例。

Object A -> Object b -> Object C 对象A->对象b->对象C

Object A has the main input available which has the extra information and Object C is where the exception is thrown. 对象A具有可用的主要输入,该输入具有额外的信息,对象C是引发异常的位置。 Also, Object C is an adapter that talks to an external system and at the moment we don't have the option to carry our input from Object A through C. 同样,对象C是与外部系统对话的适配器,目前我们没有选择将来自对象A的输入传递给C。

When exception is thrown, the thread goes into an exception mapper from where the Response is returned. 引发异常时,线程将进入异常映射器,并从该映射器返回响应。

Is there a way to make the input available in the exception mapper via some dropwizard/jersey/guice annotation magic ? 有没有办法通过一些dropwizard / jersey / guice注释魔术使输入在异常映射器中可用?

I've been able to do this by defining a RequestScoped (@RequestScoped) bean that can hold intermediary result and then injecting it into the dropwizard exception mapper using @Inject. 通过定义可以保存中间结果的RequestScoped(@RequestScoped)bean,然后使用@Inject将其注入dropwizard异常映射器中,我已经能够做到这一点。 Google Guice magic. Google Guice魔术。

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

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