Unable to inject HttpServletRequest into jakarta rest service resource

Viewed 96

I have a simple REST resource:

@Path("/test")
public class HelloResource {

    @Context HttpServletRequest request;

    @GET
    @Produces("text/plain")
    public String hello() {
        return "Hello, World! " + request.getServletContext();
    }
}

Deployed on a tomcat 10.6 server, with the following dependencies specified in gradle:

dependencies {
    implementation('jakarta.servlet:jakarta.servlet-api:5.0.0')
    implementation('org.glassfish.jersey.containers:jersey-container-servlet:3.0.1')
    implementation('org.glassfish.jersey.media:jersey-media-json-jackson:3.0.1')
    implementation('org.glassfish.jersey.inject:jersey-cdi2-se:3.0.1')
    implementation('org.jboss.weld.se:weld-se-core:4.0.0.Final')
}

NOTE: I have tried to have both the servlet API and the weld dependencies specified as compileOnly instead of implementation, but that doesn't help either. Nor do I believe this to be correct on a tomcat server.

This results in the following exception:

org.jboss.weld.exceptions.AmbiguousResolutionException: WELD-001335: Ambiguous dependencies for type Ref<HttpServletRequest> with qualifiers @Default
 Possible dependencies: 
  - org.glassfish.jersey.inject.cdi.se.bean.SupplierInstanceBeanBridge@10cc0bf,
  - org.glassfish.jersey.inject.cdi.se.bean.SupplierInstanceBeanBridge@273e6116

I have tried playing around with dependencies and reducing the code to the bare minimum required in order to isolate the problem, but no matter what I do I get this exception. This used to work fine for old (non-jakarta) rest services, but for jakarta I just cannot make it work.

0 Answers
Related