Injecting HttpRequest in RESTEasy Reactive / Quarkus fails

I’m currently trying to inject and read out the HttpRequest in Quarkus 1.13 but without any success. I’m using RESTEasy-Reactive for my endpoint.

This is how I’m currently including it

@Path("/users/{id}")
class UserController(
    @Inject val service: UserService,
    @Context val httpRequest: io.vertx.core.http.HttpServerRequest,
) 
...

The build process succeeds but when I try to access a property like httpRequest.absoluteURI() I am getting an NPE

java.lang.NullPointerException: Cannot invoke "org.jboss.resteasy.reactive.server.core.ResteasyReactiveRequestContext.serverRequest()" because the return value of "org.jboss.resteasy.reactive.server.core.CurrentRequestManager.get()" is null
    at io.quarkus.resteasy.reactive.server.runtime.QuarkusContextProducers.httpServerRequest(QuarkusContextProducers.java:26)
    at io.quarkus.resteasy.reactive.server.runtime.QuarkusContextProducers_Subclass.httpServerRequest$$superaccessor3(QuarkusContextProducers_Subclass.zig:451)
    at io.quarkus.resteasy.reactive.server.runtime.QuarkusContextProducers_Subclass$$function$$3.apply(QuarkusContextProducers_Subclass$$function$$3.zig:29)
    ...

I also tried other classes like io.vertx.mutiny.core.http.HttpServerRequest or java.net.http.HttpRequest but still without success. Injecting it with @Inject didn’t even build. I’m missing the HttpServletRequest class :/

Anybody got an idea?

Answer

You have a few options:

Using HttpFilter: https://javaee.github.io/javaee-spec/javadocs/javax/servlet/http/HttpFilter.html

@WebFilter(urlPatterns = "/*")
public class FilterEverything extends HttpFilter {


    @Override
    protected void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws IOException, ServletException {

        //Do something with HttpServletRequest 
    }

}

Using ContainerRequestFilter: https://docs.oracle.com/javaee/7/api/javax/ws/rs/container/ContainerRequestFilter.html

As Quarkus Documentation Showcases:

@Provider
public class LoggingFilter implements ContainerRequestFilter {

    private static final Logger LOG = Logger.getLogger(LoggingFilter.class);

    @Context
    UriInfo info;

    @Context
    HttpServerRequest request;

    @Override
    public void filter(ContainerRequestContext context) {

        //Do whatever you want
    }
}

As part of the method signature:

@GET
@Path("/someEndPoint")
@Produces("application/json")
public JsonObject getData(@PathParam("owner") String owner, @Context HttpServletRequest request) {
   //Do something here
}