如何将Grizzly请求注入Jersey ContainerRequestFilter [英] How to inject Grizzly Request into Jersey ContainerRequestFilter

查看:132
本文介绍了如何将Grizzly请求注入Jersey ContainerRequestFilter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有泽西由Grizzly提供。

I have Jersey being provided by Grizzly.

我有一个 ContainerRequestFilter 实现类。但是,为所有传入请求创建此类一次。因此这样做:

I have a ContainerRequestFilter implementation class. However this class is created once for all incoming requests. Therefore doing this:

public class EndpointRequestFilter implements ContainerRequestFilter {
    @Context
    private org.glassfish.grizzly.http.server.Request requestContext;

    public void filter( ContainerRequestContext req ) throws IOException {
       // remove for sake of example
    }
}

requestContext 为空。我可以将上下文注入到被调用的实际端点中,但这是相当粗糙和丑陋的,对我来说真的没用;因为我想记录各种要求。理想情况下,希望在请求的ResponseFilter端获取此$ code> Request 对象。

The requestContext is null. I can inject the context into the actual endpoint being called, but that is rather crude and ugly and really no use to me; as i wish to log various requests. Ideally would like to get at this Request object at the ResponseFilter side of the request.

必须有一个这样做的简单方法。到目前为止,我看到的所有问题/答案都不适用于Grizzly或注入REST端点调用的方法。我不想绕过我所有的数百种方法,只是因为我想得到IP地址!

There has to be an easy way of doing this. All the questions/answers I have seen thus far doesn't work for Grizzly or injects at the method being called by the REST endpoint. I don't wish to go around all my hundreds of methods adding this in call just because I want to get the IP address!

那么这里的关键是什么?我错过了什么?

So what is the key here? What am I missing?

推荐答案

我很惊讶你甚至让应用程序运行,以便找到你能找到的地方out请求为null。每当我尝试运行它时,我会在启动时遇到异常,说没有请求范围,因此无法注入请求,这就是我的预期。虽然我无法重现NPE,但我认为这个解决方案仍然可以解决您的问题。

I'm surprised you even got the app running, to get to the point where you could find out that request is null. Whenever I tried to run it, I would get an exception on start up, saying that there is no request scope, so the request can't be injected, which is what I expected. Though I couldn't reproduce the NPE, I'm thinking this solution will still solve your problem.

所以请求是一个请求范围对象,因为它在每个请求上都会更改。但过滤器本质上是一个单身人士。所以你需要做的是懒洋洋地检索它。为此,我们可以使用 javax.inject .Provider ,作为一种懒惰的检索机制。

So the Request is a request scoped object, as it changes on every request. But the filter is by its nature, a singleton. So what you need to do, is lazily retrieve it. For that, we can use javax.inject.Provider, as a lazy retrieval mechanism.

回到我的第一段中的点,这是例外我启动了

Going back to the point in my first paragraph, this was the exception I got on start up


java.lang.IllegalStateException:不在请求范围内。

java.lang.IllegalStateException: Not inside a request scope.

这是有道理的,因为 Request 需要与请求范围相关联,并且在启动时没有。请求范围仅在请求期间出现。

This makes sense, as the Request need to be associated with a request scope, and on start up, there is none. A request scope is only present during a request.

那么使用提供商的是,允许我们尝试当有请求范围时,抓住请求

So what using the Provider does, is allow us to try and grab the Request when there is a request scope present.

public static class Filter implements ContainerRequestFilter {

    @Context
    private javax.inject.Provider<Request> requestProvider;

    @Override
    public void filter(ContainerRequestContext requestContext) throws IOException {
        final Request request = requestProvider.get();
        System.out.println(request.getRemoteAddr());
    } 
}

我已经测试了这个并且按预期工作。

I've tested this and it works as expected.

另见:

  • Injecting Request Scoped Objects into Singleton Scoped Object with HK2 and Jersey

这篇关于如何将Grizzly请求注入Jersey ContainerRequestFilter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆