ResourceFilter 的依赖注入不起作用?
我有一堆 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我是个白痴。
向
LoggingResourceFilter
添加@Stateless
注释修复了该问题。Ok, I'm an idiot.
Adding a
@Stateless
annotation to theLoggingResourceFilter
fixed it.