ResourceFilter 的依赖注入不起作用?

发布于 2024-11-28 05:49:09 字数 1626 浏览 0 评论 0原文

我有一堆 JAX-RS 资源,它们为新的 WebService 提供 API。为了了解正在发生的情况,我想将有关每个请求的信息存储在数据仓库中。在我看来,这是一个横切关注点的完美示例,可以通过 ResourceFilter 来实现,对吗?

因此,我构建了一个 DataWarehouseService ,它应该在数据库中存储内容:

@Stateless
@LocalBean
public class DataWarehouseService {

    public void logApiCall(ContainerRequest cr) {
        // get interesting fields from request, store in DB
        ...
    }
}

这是我的过滤器:

public class LoggingResourceFilter implements ResourceFilter {

    Logger log = Logger.getLogger(this.getClass().getName());

    @EJB
    DataWarehouseService dwh;

    @Override
    public ContainerRequestFilter getRequestFilter() {

        return new ContainerRequestFilter() {
            @Override
            public ContainerRequest filter(ContainerRequest cr) {
                log.info("Incoming request: "+
                        cr.getHeaderValue("user-agent") +" "+
                        cr.getMethod() +" "+
                        cr.getPath()
                        );

                dwh.logApiRequest(cr);
                return cr;
            }
        };
    }

    @Override
    public ContainerResponseFilter getResponseFilter() {
        return null;
    }
}

我通过注释将过滤器注入到我的 JAX-RS 资源中 @ResourceFilters(LoggingResourceFilter.class)< /code> 在类级别。

过滤器注入工作正常,当我访问 JAX-RS 资源之一时,日志语句(“传入请求:...”)被执行。但紧接着,对注入的 DataWarehouseService 的 dwh.logApiRequest(cr) 的调用失败并出现空指针 - 显然注入失败了?!

这里有什么问题?我认为,ResourceFilters 是托管的并且可以使用 CDI。我错了吗?

所有这些都运行在 Glassfish 3.1.1 上,Jersey 1.8 是 JAX-RS 提供者。如果我使用 @Inject 会有什么不同吗?

I have a bunch of JAX-RS resources that provide an API for a new WebService. In order to understand what's happening, I'd like to store information about each request in a data warehouse. In my mind, this is a perfect example for a cross-cutting concern, which could be implemented by a ResourceFilter, right?

So I built a DataWarehouseService which is supposed to store stuff in the DB:

@Stateless
@LocalBean
public class DataWarehouseService {

    public void logApiCall(ContainerRequest cr) {
        // get interesting fields from request, store in DB
        ...
    }
}

And here's my filter:

public class LoggingResourceFilter implements ResourceFilter {

    Logger log = Logger.getLogger(this.getClass().getName());

    @EJB
    DataWarehouseService dwh;

    @Override
    public ContainerRequestFilter getRequestFilter() {

        return new ContainerRequestFilter() {
            @Override
            public ContainerRequest filter(ContainerRequest cr) {
                log.info("Incoming request: "+
                        cr.getHeaderValue("user-agent") +" "+
                        cr.getMethod() +" "+
                        cr.getPath()
                        );

                dwh.logApiRequest(cr);
                return cr;
            }
        };
    }

    @Override
    public ContainerResponseFilter getResponseFilter() {
        return null;
    }
}

I inject the filter into my JAX-RS resources via annotation @ResourceFilters(LoggingResourceFilter.class) on class-level.

The Filter-injection works fine, when I access one of the JAX-RS resources, the log statement ("Incoming request: ...") gets executed. But immediately afterwards, the call to the injected DataWarehouseService's dwh.logApiRequest(cr) fails with a Nullpointer - obviously the injection failed?!

What's the problem here? I thought, ResourceFilters are managed and can use CDI. Am I wrong?

All of this runs on Glassfish 3.1.1, Jersey 1.8 is the JAX-RS provider. Would it make a difference if I used @Inject?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忆伤 2024-12-05 05:49:09

好吧,我是个白痴。

LoggingResourceFilter 添加 @Stateless 注释修复了该问题。

Ok, I'm an idiot.

Adding a @Stateless annotation to the LoggingResourceFilter fixed it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文