4

After much trial and error, I was able to create a Jersey application with DI enabled. This is what I have in the pom.xml file:

<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>

As far as I know, the cdi-api is an API for DI, weld-servlet-shaded is its implementation in the Servlet environment. The jersey-media-json-jackson is for transforming Java classes to JSON. The jersey-container-servlet and the jersey-server set up Jersey.

The jersey-hk2 looks like some Oracle's implementation for DI, an alternative to CDI, and jersey-cdi1x-servlet enables it in Servlet environment.

According to How to integrate JAX-RS with CDI in a Servlet 3.0 container, jersey-gf-cdi-ban-custom-hk2-binding was needed to disable something (?), but I did not need this.

My questions are, why use jersey-hk2 when we have weld-servlet-shaded? Why don't they clash? Is it possible to set it up differently?

Jan Bodnar
  • 10,969
  • 6
  • 68
  • 77

0 Answers0