0

当我尝试从另一个自定义提供程序中引用一个自定义提供程序时,Jersey 会引发异常。

这个测试用例

public class NestedProvidersTest extends JerseyTest {
    public NestedProvidersTest() throws TestContainerException {
        super(new LowLevelAppDescriptor.Builder(new DefaultResourceConfig(Resource.class, ParentProvider.class, ChildProvider.class)).build());
    }

    @Test public void test() {
        resource().path("/").get(String.class);
    }

    @Path("/") public static class Resource {
        @Context private Parent parent;

        @GET public String get() {
            return "hello";
        }
    }

    public interface Parent {}
    public interface Child {}

    @Provider public static class ParentProvider extends PerRequestTypeInjectableProvider<Context, Parent> {
        @Context private Child child;

        public ParentProvider() {
            super(Parent.class);
        }

        @Override public Injectable<Parent> getInjectable(ComponentContext ic, Context context) {
            return new Injectable<Parent>() {
                @Override public Parent getValue() {
                    return new Parent(){};
                }
            };
        }
    }

    @Provider public static class ChildProvider extends PerRequestTypeInjectableProvider<Context, Child> {
        public ChildProvider() {
            super(Child.class);
        }

        @Override public Injectable<Child> getInjectable(ComponentContext ic, Context context) {
            return new Injectable<Child>() {
                @Override public Child getValue() {
                    return new Child(){};
                }
            };
        }
    }
}

给了我例外

Apr 05, 2013 7:02:41 PM com.sun.jersey.test.framework.spi.container.grizzly2.GrizzlyTestContainerFactory$GrizzlyTestContainer <init>
INFO: Creating low level grizzly2 container configured at the base URI http://localhost:9998/
Apr 05, 2013 7:02:41 PM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFO: Initiating Jersey application, version 'Jersey: 1.17.1 02/28/2013 12:47 PM'
Apr 05, 2013 7:02:43 PM com.sun.jersey.spi.inject.Errors processErrorMessages
SEVERE: The following errors and warnings have been detected with resource and/or provider classes:
  SEVERE: Missing dependency for field: private NestedProvidersTest$Child NestedProvidersTest$ParentProvider.child

com.sun.jersey.test.framework.spi.container.TestContainerException: com.sun.jersey.spi.inject.Errors$ErrorMessagesException
    at com.sun.jersey.test.framework.spi.container.grizzly2.GrizzlyTestContainerFactory$GrizzlyTestContainer.<init>(GrizzlyTestContainerFactory.java:106)
    at com.sun.jersey.test.framework.spi.container.grizzly2.GrizzlyTestContainerFactory.create(GrizzlyTestContainerFactory.java:77)
    at com.sun.jersey.test.framework.JerseyTest.getContainer(JerseyTest.java:345)
    at com.sun.jersey.test.framework.JerseyTest.<init>(JerseyTest.java:220)
    at NestedProvidersTest.<init>(NestedProvidersTest.java:22)
[snip]
Caused by: com.sun.jersey.spi.inject.Errors$ErrorMessagesException
    at com.sun.jersey.spi.inject.Errors.processErrorMessages(Errors.java:170)
    at com.sun.jersey.spi.inject.Errors.postProcess(Errors.java:136)
    at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:199)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:770)
    at com.sun.jersey.api.container.ContainerFactory.createContainer(ContainerFactory.java:172)
    at com.sun.jersey.api.container.ContainerFactory.createContainer(ContainerFactory.java:134)
    at com.sun.jersey.test.framework.spi.container.grizzly2.GrizzlyTestContainerFactory$GrizzlyTestContainer.<init>(GrizzlyTestContainerFactory.java:102)
    ... 29 more

我做错了什么,还是泽西岛不支持这种行为?

4

1 回答 1

1

更合适的问题应该是: is this behavior not supported by Jersey 1.17?

我不想玩猜谜游戏,所以我只能告诉你,这可以用 Jersey 2 来完成。

为什么它在 1.17 中不起作用?可能是因为Child在注册提供程序期间该依赖项不可用。

Jersey 2 使用 JSR-330 即 @Inject 和所有这些以及做某事的方法是使用javax.inject.Provider

然后,您将使用某种活页夹来注册注射剂,但在使用 Provider 时跳过所有细节,您不希望在注册期间注入 T 。稍后会在Provider#get

请求范围过滤器的示例:

@Inject
private Provider<ResourceInfo> resourceInfo;

如果我只想做这些:

@Inject
ResourceInfo resourceInfo

那么它就会失败,就像它对你失败一样。

如果你可以升级到2,那么我认为这是值得的。

于 2013-04-10T08:46:19.837 回答