在 RESTEasy Reactive / Quarkus 中注入 HttpRequest 失败

Injecting HttpRequest in RESTEasy Reactive / Quarkus fails

我目前正在尝试在 Quarkus 1.13 中注入和读出 HttpRequest,但没有成功。我正在为我的端点使用 RESTEasy-Reactive。

这就是我目前包含它的方式

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

构建过程成功,但是当我尝试访问 属性 之类的 httpRequest.absoluteURI() 时,我遇到了 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$.apply(QuarkusContextProducers_Subclass$$function$.zig:29)
    ...

我也尝试了其他 class,例如 io.vertx.mutiny.core.http.HttpServerRequestjava.net.http.HttpRequest,但仍然没有成功。用 @Inject 注入它甚至没有构建。我错过了 HttpServletRequest class :/

有人知道吗?

您有几个选择:

使用 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 
    }

}

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

如 Quarkus 文档所示:

@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
    }
}

作为方法签名的一部分:

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