经过多次试验和错误,我能够创建启用了DI的Jersey应用程序。这就是我在pom.xml
文件中的内容:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>2.26</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext.cdi</groupId>
<artifactId>jersey-cdi1x-servlet</artifactId>
</dependency>
<!--dependency>
<groupId>org.glassfish.jersey.ext.cdi</groupId>
<artifactId>jersey-weld2-se</artifactId>
</dependency -->
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>2.0-EDR1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.weld.servlet</groupId>
<artifactId>weld-servlet-shaded</artifactId>
<version>3.0.2.Final</version>
</dependency>
<!-- dependency>
<groupId>org.glassfish.jersey.containers.glassfish</groupId>
<artifactId>jersey-gf-cdi-ban-custom-hk2-binding</artifactId>
<version>2.14</version>
</dependency -->
</dependencies>
据我所知,cdi-api
是DI的API,weld-servlet-shaded
是它在Servlet环境中的实现。 jersey-media-json-jackson
用于将Java类转换为JSON。 jersey-container-servlet
和jersey-server
设置泽西岛。
jersey-hk2
看起来像是Oracle的DI实现,CDI的替代品,jersey-cdi1x-servlet
在Servlet环境中启用它。
根据How to integrate JAX-RS with CDI in a Servlet 3.0 container,需要jersey-gf-cdi-ban-custom-hk2-binding
来禁用某些内容(?),但我不需要这样做。
我的问题是,为什么在jersey-hk2
时使用weld-servlet-shaded
?他们为什么不冲突?是否有可能以不同的方式进行设置?