Reputation:
I need to use Jersey 2.28 (with Jetty) without CDI container in SE environment. All my settings are in web.xml:
<servlet>
<servlet-name>JerseyServlet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.noname.rest</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JerseyServlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
These are the dependencies I use:
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
cdi-api-2.0.jar
javax.inject-2.5.0-b62.jar
This is my resource:
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("myresource")
public class MyResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getIt() {
return "Got it!";
}
}
And this is what I get:
java.lang.IllegalStateException: InjectionManagerFactory not found.
at org.glassfish.jersey.internal.inject.Injections.lambda$lookupInjectionManagerFactory$0(Injections.java:74) ~[jersey-common-2.28.jar:?]
at java.util.Optional.orElseThrow(Optional.java:408) ~[?:?]
at org.glassfish.jersey.internal.inject.Injections.lookupInjectionManagerFactory(Injections.java:74) ~[jersey-common-2.28.jar:?]
at org.glassfish.jersey.internal.inject.Injections.createInjectionManager(Injections.java:69) ~[jersey-common-2.28.jar:?]
at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:258) ~[jersey-server-2.28.jar:?]
at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:311) ~[jersey-container-servlet-core-2.28.jar:?]
at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:154) ~[jersey-container-servlet-core-2.28.jar:?]
at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:346) ~[jersey-container-servlet-core-2.28.jar:?]
at javax.servlet.GenericServlet.init(GenericServlet.java:244) ~[javax.servlet-api-and-schemas-3.1.0.jar:?]
at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:672) [jetty-servlet-9.4.14.v20181114.jar:?]
at org.eclipse.jetty.servlet.ServletHolder.initialize(ServletHolder.java:429) [jetty-servlet-9.4.14.v20181114.jar:?]
at org.eclipse.jetty.servlet.ServletHandler.lambda$initialize$0(ServletHandler.java:750) [jetty-servlet-9.4.14.v20181114.jar:?]
I know that Jersey can be used with different DI containers, for example Weld, HK2 etc. But is it possible to use it without DI container? If yes, then how?
Upvotes: 0
Views: 1223
Reputation: 208984
No, it's not possible. Jersey only has an SPI for its dependency injection provider. It needs an implementation for it to run. Same way like JAX-RS is just a specification, but it needs an implementation (like Jersey or RESTEasy) to run. This is exactly the same thing. Jersey uses a lot of dependency injection internally, so the core code uses a facade for DI in its codebase. And we need to provide the underlying implementation for it to run. The implementations currently available, as you seen to understand, are HK2 and CDI (jersey-hk2
and jersey-cdi2-se
). I'm sure you've seen this post
Upvotes: 1