在 Jersey 生命周期中如何拦截请求?
我已经使用 Jersey 一年多了,刚刚偶然发现了一个我找不到答案的问题:如何拦截(或挂钩) Jersey 请求生命周期?
理想情况下,我能够在容器接受来自网络的请求和调用处理程序方法之间执行一些自定义过滤/验证/拒绝。如果有一种简单的方法可以按子路径过滤拦截器(例如,对 / 下的任何内容使用一个拦截器,对 /user/ 下的任何内容使用另一个拦截器,等等),那就加分了。
谢谢!
编辑:为了更清楚一点,这里的总体思想是能够编写一些将为许多 API 调用运行的代码,而不必从每个处理程序方法中显式调用该代码。这将减少额外的代码并消除传递请求上下文的需要。
I've used Jersey for the better part of a year now and have just stumbled upon a problem to which I can't find the answer: how do you intercept (or hook into) the Jersey request lifecycle?
Ideally, I'd be able to perform some custom filtering/validation/rejection between the time the container accepts the request from the network and the time my handler methods are called. Bonus points if there's an easy way to filter the interceptors by sub-path (e.g. have one interceptor for anything under /, another for anything under /user/, etc.).
Thanks!
Edit: To be a bit clearer, the general idea here is to be able to write some code that will be run for many API calls without having to explicitly call that code from each handler method. This would reduce extra code and eliminate the need to pass request contexts around.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我已经找到答案了。
首先,创建一个实现 ContainerRequestFilter 的类。该接口指定了以下方法,在该方法中进行过滤。 ContainerRequest 对象包含有关当前请求的信息。
之后,在 web.xml
源的 servlet 配置中包含以下 XML:
http://jersey.576304.n2.nabble.com/ContainerRequestFilter-and-Resources-td4419975.html
http://markmail.org/message/p7yxygz4wpakqno5
I've found the answer.
First, create a class that implements ContainerRequestFilter. The interface specifies the following method, in which the filtering takes place. The ContainerRequest object contains information about the current request.
After that, include the following XML in the servlet configuration in web.xml
Sources:
http://jersey.576304.n2.nabble.com/ContainerRequestFilter-and-Resources-td4419975.html
http://markmail.org/message/p7yxygz4wpakqno5
这个线程有点旧,但我有一段时间拦截请求之前和之后。经过在网上进行长时间搜索后,我终于弄清楚了:
然后是这个类:
它会在开始和结束时拦截请求,以便您可以放入计时器或其他内容。
这适用于 Jersey 1.17。不确定 2.x。
This thread is a bit old, but I was having a fit of a time intercepting both before and after the request. After a long search on the web, I finally figured this out:
and then this class:
This intercepts the request at the beginning and the end so you can put in a timer or whatever.
This works for Jersey 1.17. Not sure about 2.x.
对于服务器部分,我们使用 Jersey Specific 类来执行如下操作:
ContainerResponseFilter
签名是:
然后您可以执行如下调用:
这有帮助吗?..
For the server part we use a Jersey Specific class to do something like this:
ContainerResponseFilter
The signature is:
then you can do calls like:
Can this be of some help ?..
你看过球衣
ClientFilter
类?我们目前正在使用它来拦截和执行 API 版本控制等。有内置的日志过滤器 - 因此您可以查看它们的代码以了解要编写的内容。
签名是:
所以你可以从做类似的事情开始:
Have you looked at the Jersey
ClientFilter
class ?We are currently using this to intercept and perform API versioing etc.. There is built in logging filters - so you can look at the code for them to get an idea of what to write.
The signature is:
So you can start by doing stuff like: