如何混合Guice和Jersey注射剂? [英] How can I mix Guice and Jersey injection?

查看:103
本文介绍了如何混合Guice和Jersey注射剂?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过以下问题,介绍如何将@Context依赖项注入Jersey资源构造函数中.但是我的问题略有不同-我想注入一个@PathParam字符串.我有一个类似于以下内容的课程:

I've already seen the following question on how to inject @Context dependencies into Jersey resource constructors. But my question is slightly different -- I'd like to inject a @PathParam String. I have a class resembling the following:

@Path("foo/{fooId}/bar")
public class BarResource {
  @Inject
  public BarResource(@PathParam("fooId") String foo, Service service) {
    ...
  }
  ...
}

Service由Guice很好地注入,但是路径段始终为null.这实际上使我感到惊讶.如果我以为Guice会抱怨无法解决的依赖关系而大声爆炸.

The Service is injected fine by Guice, but the path segment is always null. This actually surprises me; if anything I assumed Guice would loudly explode complaining about an unresolvable dependency.

如何以这种方式注入路径参数?为了使这些资源类可以进行单元测试,我宁愿避免使用字段注入.

How can I inject a path parameter in this manner? I would prefer to avoid field injection for the purposes of keeping these resource classes unit-testable.

推荐答案

默认情况下,似乎不接受@PathParam作为Constructor自变量.此新功能文档指出

It appears that @PathParam is not acceptable by default as a Constructor argument. This new feature document states

资源类构造函数中允许的参数取决于用于创建资源类实例的资源提供者[...].对于默认的按请求资源类,您可以使用带UriParam,UriParam,QueryParam,MatrixParam,HeaderParam或HttpContext注释的参数的任意组合.

The arguments allowed in a resource class constructor depends on the resource provider used to create an instance of the resource class [...]. For default per-request resource classes you can use any combination of parameters annotated with UriParam, UriParam, QueryParam, MatrixParam, HeaderParam or HttpContext.

您可以提供自己的资源提供者来处理@PathParam注释.

You could provide your own resource provider that processes the @PathParam annotation.

这篇关于如何混合Guice和Jersey注射剂?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆