我正在尝试将我的Jersey项目从2.25更新为 2.27 。
Jersey自2.26版以来就是Java EE 8的一部分,所以我还决定将我的项目从Java EE 7更新为 Java EE 8 ,以及从JAX-RS 2.0 / CDI 1。 x / Weld2.x / Apache Tomcat 8到 JAX-RS 2.1 / CDI 2.0 / Weld 3.0 / Apache Tomcat 9 。
问题是我无法弄清我需要包含在项目中的确切依赖(Maven项目,pom.xml
)。
我尝试过的事情:
cdi-api
,jersey-hk2
,javax.inject
,jersey-cdi1x
,jersey-cdi1x-servlet
, weld-servlet
,weld-servlet-core
,weld-servlet-shaded
,weld-core-impl
这些是我当前的依赖项(pom.xml
):
<!-- Java EE -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>${java-ee.version}</version>
</dependency>
<!-- JAX-RS -->
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>${jaxrs.version}</version>
</dependency>
<!-- Jersey -->
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>${jersey.version}</version>
</dependency>
<!-- CDI (Jersey HK2 / Weld) -->
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.weld.servlet</groupId>
<artifactId>weld-servlet-shaded</artifactId>
<version>${weld.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.weld</groupId>
<artifactId>weld-core-impl</artifactId>
<version>${weld.version}</version>
</dependency>
这是我在尝试将@Singleton
注入资源(@Path
)时遇到的错误:
ADVERTENCIA: The following warnings have been detected: WARNING: Unknown HK2 failure detected:
...
GRAVE: El Servlet.service() para el servlet [CGERestApplication] en el contexto con ruta [/igea-cge-interfaces] lanzó la excepción [A MultiException has 3 exceptions. They are:
1. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=SapClient,parent=CambioEstadoTrabajoService,qualifiers={},position=-1,optional=false,self=false,unqualified=null,363959695)
2. java.lang.IllegalArgumentException: While attempting to resolve the dependencies of es.indra.isl.igea.external.cge.web.api.services.sap.CambioEstadoTrabajoService errors were found
3. java.lang.IllegalStateException: Unable to perform operation: resolve on es.indra.isl.igea.external.cge.web.api.services.sap.CambioEstadoTrabajoService
] with root cause:
org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=SapClient,parent=CambioEstadoTrabajoService,qualifiers={},position=-1,optional=false,self=false,unqualified=null,363959695)
at org.jvnet.hk2.internal.ThreeThirtyResolver.resolve(ThreeThirtyResolver.java:75)
at org.jvnet.hk2.internal.ClazzCreator.resolve(ClazzCreator.java:212)
at org.jvnet.hk2.internal.ClazzCreator.resolveAllDependencies(ClazzCreator.java:235)
at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:358)
at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:487)
at org.glassfish.jersey.inject.hk2.RequestContext.findOrCreate(RequestContext.java:83)
at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2126)
at org.jvnet.hk2.internal.ServiceLocatorImpl.internalGetService(ServiceLocatorImpl.java:777)
at org.jvnet.hk2.internal.ServiceLocatorImpl.internalGetService(ServiceLocatorImpl.java:740)
at org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:710)
at org.glassfish.jersey.inject.hk2.AbstractHk2InjectionManager.getInstance(AbstractHk2InjectionManager.java:184)
at org.glassfish.jersey.inject.hk2.ImmediateHk2InjectionManager.getInstance(ImmediateHk2InjectionManager.java:54)
at org.glassfish.jersey.internal.inject.Injections.getOrCreate(Injections.java:129)
at org.glassfish.jersey.server.model.MethodHandler$ClassBasedMethodHandler.getInstance(MethodHandler.java:284)
at org.glassfish.jersey.server.internal.routing.PushMethodHandlerRouter.apply(PushMethodHandlerRouter.java:75)
at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:110)
Tomcat启动日志:
jun 04, 2018 10:09:07 AM org.jboss.weld.environment.servlet.EnhancedListener onStartup INFO: WELD-ENV-001008: Initialize Weld using ServletContainerInitializer jun 04, 2018 10:09:08 AM org.jboss.weld.bootstrap.WeldStartup INFO: WELD-000900: 3.0.4 (Final) jun 04, 2018 10:09:08 AM org.jboss.weld.bootstrap.WeldStartup startContainer INFO: WELD-000101: Transactional services not available. Injection of @Inject UserTransaction not available. Transactional observers will be invoked synchronously. jun 04, 2018 10:09:09 AM org.jboss.weld.environment.tomcat.TomcatContainer initialize INFO: WELD-ENV-001100: Tomcat 7+ detected, CDI injection will be available in Servlets, Filters and Listeners.
答案 0 :(得分:0)
经过一些研究后,我发现我在问题中列出的依赖关系是正确的。
我遗失的一件事是在我的AbstractBinder
中注册ResourceConfig
,如this answer和Jersey user guide中所述。
示例:强>
public class MyApplication extends ResourceConfig {
public MyApplication() {
super();
// ...
register(new MyInjectionBinder());
}
}
@Singleton
public class MyInjectable {
// ...
}
public class MyInjectionBinder extends AbstractBinder {
@Override
protected void configure() {
bind(MyInjectable.class).to(MyInjectable.class).in(Singleton.class);
}
}
@Path("/myresource")
public class MyResource {
@Inject
MyInjectable injectable;
// ...
}
另请注意,如果您想将其他注射剂注入您的注射剂,那么在 postconstruct 生命周期事件之前它们将无法使用:
@Singleton
public class MyInjectable {
@Inject
OtherInjectable other;
@PostConstruct
private void init() {
// ... do something with "other"
}
}
答案 1 :(得分:0)
我解决了这个问题。我正在使用Jetty 9.4.14,Jersey 2.28和Weld(servlet着色)3.1.0。这是Java 11依赖项的列表。
//Jersey:
jaxb-api-2.3.0.jar
jaxb-core-2.3.0.1.jar
jaxb-impl-2.3.0.1.jar
javax.ws.rs-api-2.1.1.jar
javax.activation-1.2.0.jar
jersey-container-jetty-http-2.28.jar
jersey-container-servlet-core-2.28.jar
jersey-server-2.28.jar
jersey-common-2.28.jar
jersey-client-2.28.jar
//Jersey + CDI:
jersey-bean-validation-2.28.jar
bean-validator-2.5.0-b06.jar
jersey-cdi1x-2.28.jar
jersey-cdi1x-servlet-2.28.jar
jersey-hk2-2.28.jar
hk2-api-2.5.0.jar
hk2-utils-2.5.0.jar
hk2-locator-2.5.0.jar
您可以在泽西岛issue中找到更多信息。