How to fix "org.glassfish.jersey.inject.hk2.RequestContext.RequestContext must be in the Singleton scope"

Viewed 636

I'd like to implement a simple REST-API with Jersey and grizzly. After beeing stuck in dependency-hell for a while, I ended up with an exception and I have no idea how to handle/interpret:

java.lang.IllegalArgumentException: The implementation class org.glassfish.jersey.inject.hk2.RequestContext must be in the Singleton scope
    at org.jvnet.hk2.internal.ServiceLocatorImpl.checkConfiguration(ServiceLocatorImpl.java:1713)
    at org.jvnet.hk2.internal.ServiceLocatorImpl.addConfiguration(ServiceLocatorImpl.java:2108)
    at org.jvnet.hk2.internal.DynamicConfigurationImpl.commit(DynamicConfigurationImpl.java:262)
    at org.glassfish.hk2.utilities.ServiceLocatorUtilities.bind(ServiceLocatorUtilities.java:166)
    at org.glassfish.jersey.inject.hk2.AbstractHk2InjectionManager.<init>(AbstractHk2InjectionManager.java:65)
    at org.glassfish.jersey.inject.hk2.ImmediateHk2InjectionManager.<init>(ImmediateHk2InjectionManager.java:38)
    at org.glassfish.jersey.inject.hk2.Hk2InjectionManagerFactory$Hk2InjectionManagerStrategy$1.createInjectionManager(Hk2InjectionManagerFactory.java:55)
    at org.glassfish.jersey.inject.hk2.Hk2InjectionManagerFactory.create(Hk2InjectionManagerFactory.java:73)
    at org.glassfish.jersey.internal.inject.Injections.createInjectionManager(Injections.java:69)
    at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:259)
    at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:246)
    at org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpContainer.<init>(GrizzlyHttpContainer.java:310)
    at org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory.createHttpServer(GrizzlyHttpServerFactory.java:93)
    at src.main.java.myApplication.main(myApplication.java:141)

The exception happens in my main-class on creating the http-server:

URI uri= URI.create("http://localhost:8080");       
ResourceConfig config = new ResourceConfig();
config.register(myAPI.class);

HttpServer server = GrizzlyHttpServerFactory.createHttpServer(uri, config);
try{
    server.start();
} catch( IOException e ) {
    logger.error("Exception on starting HTTP-Server.", e);
}

The myAPI.java is nothing special yet and also not important for the problem here, I think. Nevertheless, it looks like this:

public class myAPI extends ResourceConfig{
    private static final ObjectMapper mapper = new ObjectMapper();

    public myAPI(){
    }

    @Path("/hello")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response foo() {
        ObjectNode json = mapper.createObjectNode();
        json.put("status", "ok");
        return Response.status(Response.Status.OK).entity(json).build();
    }    
}

My classpath contains the following huge amount of libraries:

commons-logging-1.1.3.jar
grizzly-framework-3.0.0.jar
grizzly-http-server-3.0.0.jar
grizzly-http-server-multipart-3.0.0.jar
hk2-api-2.6.1.jar
hk2-locator-2.6.1.jar
hk2-utils-2.6.1.jar
jakarta.annotation-api-2.0.0.jar
jakarta.inject-api-2.0.0.jar
jakarta.ws.rs-api-3.0.0.jar
javax.annotation-api-1.3.2.jar
javax.inject-1.jar
jersey-common-3.0.2.jar
jersey-container-grizzly2-http-3.0.2.jar
jersey-guava-2.22.1.jar
jersey-hk2-3.0.2.jar
jersey-server-3.0.2.jar
org.apache.commons.logging-1.1.3.jar
jackson-annotations-2.9.0.jar
jackson-core-2.9.4.jar
jackson-databind-2.9.4.jar
-
httpclient-4.5.13.jar
httpcore-4.4.14.jar
httpmime-4.5.13.jar
log4j-api-2.14.1.jar
log4j-core-2.14.1.jar

The libraries below the dash are needed for another module (http-client) inside my application. I know that I probably could have used some of the libraries there, which I also use for the http-server, but I couldn't get it to work otherwise. But that's not the focus here, only when this is responsible for some kind of collusions due to multiple implementations?! After the long trail-and-error in dependency-hell, it is also possible that some libraries are even not needed.

So the core-question is, how I can put the RequestContext into the singleton-scope?! I haven't even declared a RequestContext anywhere. Do I have to add it in some way?

Side-fact: I also tried the same with the Jersey-JdkHttpServerFactory and ended up with the same error.

0 Answers
Related