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
我做错了什么,或者泽西不支持这种行为?
答案 0 :(得分:1)
更合适的问题应该是:
is this behavior not supported by Jersey 1.17?
我不想玩猜谜游戏,所以我只能告诉你它可以用泽西2来完成。
为什么它在1.17中不起作用?可能是因为在提供者注册期间无法获得Child
依赖性。
Jersey 2使用@Inject的JSR-330,所有这些以及你想要做的事情的方法是使用javax.inject.Provider。
然后你会使用某种类型的活页夹来注册注射剂,但是当你使用提供者时你不会在注册期间注入T时跳过所有细节。稍后将在Provider#get
请求范围过滤器的示例:
@Inject
private Provider<ResourceInfo> resourceInfo;
如果我想这样做:
@Inject
ResourceInfo resourceInfo
然后就会因为你失败而失败。
如果你可以升级到2,那么我认为这是值得的。