207

I am receiving below error while running my Jersey API in Tomcat 8.5.11 which is causing my API to stop:

HTTP Status 500 - Servlet.init() for servlet Jersey REST Service threw exception

type Exception report

message Servlet.init() for servlet Jersey REST Service threw exception

description The server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Servlet.init() for servlet Jersey REST Service threw exception org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:474) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349) org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:783) org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:798) org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1434) org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) java.lang.Thread.run(Thread.java:745)

root cause

java.lang.IllegalStateException: InjectionManagerFactory not found. org.glassfish.jersey.internal.inject.Injections.lookupInjectionManagerFactory(Injections.java:97) org.glassfish.jersey.internal.inject.Injections.createInjectionManager(Injections.java:89) org.glassfish.jersey.server.ApplicationHandler.(ApplicationHandler.java:282) org.glassfish.jersey.servlet.WebComponent.(WebComponent.java:335) org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:178) org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:370) javax.servlet.GenericServlet.init(GenericServlet.java:158) org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:474) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349) org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:783) org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:798) org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1434) org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) java.lang.Thread.run(Thread.java:745)

The application is build with the following dependencies with gradle:

dependencies {
    compile (
        // REST
        "org.glassfish.jersey.containers:jersey-container-servlet:2.+",
        "javax.servlet:javax.servlet-api:4.+",
        // REST Token
        "org.bitbucket.b_c:jose4j:0.+",
        // MongoDB
        "org.hibernate.ogm:hibernate-ogm-bom:5.+",
        "org.hibernate.ogm:hibernate-ogm-infinispan:5.+",
        "org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.+",
        "org.jboss.spec.javax.transaction:jboss-transaction-api_1.2_spec:1.+",
        "org.jboss.narayana.jta:narayana-jta:5.+",
        "org.jboss:jboss-transaction-spi:7.+",
        "log4j:log4j:1.+",
        "org.hibernate.ogm:hibernate-ogm-mongodb:5.+",
        "org.bouncycastle:bcprov-jdk15on:1.+"
    ) }

This downloads jersey-common-2.26-b04.jar which contains the missing class under /org/glassfish/jersey/internal/inject/InjectionManagerFactory. The jar file is deployed into the Tomcat folder under WEB-INF/lib

What can be wrong here? The gradle script worked the last few month with the same Tomcat version.

Paolo Forgia
  • 6,572
  • 8
  • 46
  • 58
gregor
  • 2,397
  • 2
  • 12
  • 18
  • 1
    i see there was a new version of jersey on 19/05 - check if this is the problem, i have the same problem currently – Roman Kesler May 21 '17 at 11:37
  • https://search.maven.org/#search|gav|1|g%3A%22org.glassfish.jersey.core%22%20AND%20a%3A%22jersey-common%22 – Roman Kesler May 21 '17 at 11:43
  • This tutorial helped me fix this issue http://crunchify.com/how-to-build-restful-service-with-java-using-jax-rs-and-jersey/ – JesseBoyd Oct 26 '17 at 01:34

7 Answers7

392

Add this dependency:

<dependency>
    <groupId>org.glassfish.jersey.inject</groupId>
    <artifactId>jersey-hk2</artifactId>
    <version>2.28</version>
</dependency>

cf. https://stackoverflow.com/a/44536542/1070215

Make sure not to mix your Jersey dependency versions. This answer says version "2.28", but use whatever version your other Jersey dependency versions are.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
cthiebaud
  • 4,917
  • 2
  • 19
  • 8
  • 2
    Worked for me with 2.26 release version. Didn't want to use beta versions in production code. – saganas Nov 12 '17 at 12:52
  • 2
    Thank you, this is the correct answer. Works with 2.26 – mario Nov 16 '17 at 10:14
  • 2
    [See also](https://stackoverflow.com/a/51217530/2587435) for explanation. – Paul Samsotha Jul 17 '18 at 06:52
  • 2
    This was it for me - version 2.28. – absmiths Feb 13 '19 at 14:57
  • What was bizzare for me was that it was working, and then it stopped working UNTIL I did the above inclusion as per this post. Anyhow, thank you. My core version was 2.30 and my inject version as above i.e. 2.28. – Beezer Feb 23 '20 at 18:01
  • 1
    @Beezer No, No, don't mix your Jersey dependency versions. If your jersey-core (which you should not even need, as it's a transitive) is 2.30, then your jersey-hk2 should be 2.30. Mixing versions always leads to weird problems. – Paul Samsotha Apr 11 '20 at 21:55
  • Still works and needed for 3.0.2. Why they didn't fix this in a new major version is beyond me. – Jorn Aug 23 '21 at 13:04
  • Worked for me with jersey 2.38. – FloT Feb 08 '23 at 13:35
160

Jersey 2.26 and newer are not backward compatible with older versions. The reason behind that has been stated in the release notes:

Unfortunately, there was a need to make backwards incompatible changes in 2.26. Concretely jersey-proprietary reactive client API is completely gone and cannot be supported any longer - it conflicts with what was introduced in JAX-RS 2.1 (that's the price for Jersey being "spec playground..").

Another bigger change in Jersey code is attempt to make Jersey core independent of any specific injection framework. As you might now, Jersey 2.x is (was!) pretty tightly dependent on HK2, which sometimes causes issues (esp. when running on other injection containers. Jersey now defines it's own injection facade, which, when implemented properly, replaces all internal Jersey injection.


As for now one should use the following dependencies:

Maven

<dependency>
    <groupId>org.glassfish.jersey.inject</groupId>
    <artifactId>jersey-hk2</artifactId>
    <version>2.26</version>
</dependency>

Gradle

compile 'org.glassfish.jersey.inject:jersey-hk2:2.26'
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
wypieprz
  • 7,981
  • 4
  • 43
  • 46
  • Also see issue [Cannot upgrade from 2.25.1 to 2.26](https://github.com/jersey/jersey/issues/3664) – Grigory Kislin Dec 17 '17 at 14:03
  • 25
    Sigh... why wouldn't Jersey bump the version to 3.0 if they are making a breaking change.. – trevorism Feb 18 '18 at 18:31
  • 1
    @trevorism I have a feeling they want to keep the major version in sync with the JAX-RS major version. That's the only thing that makes sense to me. – Paul Samsotha Aug 03 '18 at 05:37
  • Also jersey-common is not a requirement (to _manually_ add). This should already be pulled in by jersey-server, which should be pulled in by jersey-container-servlet-core or any other "main" jersey dependency you use. The only required dependency to get rid of the error in question is the jersey-hk2 (or jersey-cdi2-se, as mentioned [here](https://stackoverflow.com/a/51217530/2587435)). – Paul Samsotha Dec 11 '18 at 22:07
65

Here is the reason. Starting from Jersey 2.26, Jersey removed HK2 as a hard dependency. It created an SPI as a facade for the dependency injection provider, in the form of the InjectionManager and InjectionManagerFactory. So for Jersey to run, we need to have an implementation of the InjectionManagerFactory. There are two implementations of this, which are for HK2 and CDI. The HK2 dependency is the jersey-hk2 others are talking about.

<dependency>
    <groupId>org.glassfish.jersey.inject</groupId>
    <artifactId>jersey-hk2</artifactId>
    <version>2.26</version>
</dependency>

The CDI dependency is

<dependency>
    <groupId>org.glassfish.jersey.inject</groupId>
    <artifactId>jersey-cdi2-se</artifactId>
    <version>2.26</version>
</dependency>

This (jersey-cdi2-se) should only be used for SE environments and not EE environments.

Jersey made this change to allow others to provide their own dependency injection framework. They don't have any plans to implement any other InjectionManagers, though others have made attempts at implementing one for Guice.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • 1
    Note that using CDI (jersey-cdi2-se) requires a bean.xml configuration in META-INF. Otherwise following exception will be thrown: java.lang.IllegalStateException: WELD-ENV-000016: Missing beans.xml file in META-INF – Marco Montel Feb 28 '19 at 14:27
  • This answer helpped me with so many inconsistencies, +1 for clarifying jersey-cdi2-se should only be used for SE – Daniel Arechiga Jun 22 '20 at 02:12
13

Choose which DI to inject stuff into Jersey:

Spring 4:

<dependency>
  <groupId>org.glassfish.jersey.ext</groupId>
  <artifactId>jersey-spring4</artifactId>
</dependency>

Spring 3:

<dependency>
  <groupId>org.glassfish.jersey.ext</groupId>
  <artifactId>jersey-spring3</artifactId>
</dependency>

HK2:

<dependency>
    <groupId>org.glassfish.jersey.inject</groupId>
    <artifactId>jersey-hk2</artifactId>
</dependency>
broc.seib
  • 21,643
  • 8
  • 63
  • 62
  • 2
    It's not that easy. You can't just switch out HK2 for Spring. The `jersey-spring` integration still uses a HK2 bridge under the hood to make it work. – Paul Samsotha Jul 17 '18 at 06:51
2

The only way I could solve it was via:

org.glassfish.jersey.core jersey-server ${jersey-2-version}

<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet</artifactId>
    <version>${jersey-2-version}</version>
</dependency>

<dependency>
    <groupId>org.glassfish.jersey.inject</groupId>
    <artifactId>jersey-hk2</artifactId>
    <version>${jersey-2-version}</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.core/jersey-common -->
<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-common</artifactId>
    <version>${jersey-2-version}</version>
</dependency>

<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet-core</artifactId>
    <version>${jersey-2-version}</version>
</dependency>

So, only if I added jersey-container-servlet and jersey-hk2 would it run without errors

Johannes Jander
  • 4,974
  • 2
  • 31
  • 46
0

As far as I can see dependencies have changed between 2.26-b03 and 2.26-b04 (HK2 was moved to from compile to testCompile)... there might be some change in the jersey dependencies that has not been completed yet (or which lead to a bug).

However, right now the simple solution is to stick to an older version :-)

-3

Here is the new dependency (August 2017)

    <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.core/jersey-common -->
<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-common</artifactId>
    <version>2.0-m03</version>
</dependency>