在 Jersey 生命周期中如何拦截请求?

发布于 2024-10-06 02:25:29 字数 309 浏览 9 评论 0原文

我已经使用 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 技术交流群。

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

发布评论

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

评论(4

不乱于心 2024-10-13 02:25:29

我已经找到答案了。

首先,创建一个实现 ContainerRequestFilter 的类。该接口指定了以下方法,在该方法中进行过滤。 ContainerRequest 对象包含有关当前请求的信息。

public ContainerRequest filter(ContainerRequest req);

之后,在 web.xml

<init-param>
  <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
  <param-value>path.to.filtering.class</param-value>
</init-param>

源的 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.

public ContainerRequest filter(ContainerRequest req);

After that, include the following XML in the servlet configuration in web.xml

<init-param>
  <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
  <param-value>path.to.filtering.class</param-value>
</init-param>

Sources:

http://jersey.576304.n2.nabble.com/ContainerRequestFilter-and-Resources-td4419975.html
http://markmail.org/message/p7yxygz4wpakqno5

甜扑 2024-10-13 02:25:29

这个线程有点旧,但我有一段时间拦截请求之前和之后。经过在网上进行长时间搜索后,我终于弄清楚了:

<init-param>
    <param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
    <param-value>blah.LoggingFilter</param-value>
</init-param>
<init-param>
    <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
    <param-value>blah.LoggingFilter</param-value>
</init-param>

然后是这个类:

public class LoggingFilter extends LoggingFilter implements ContainerRequestFilter {

    private static final ThreadLocal<Long> startTime = new ThreadLocal<Long>();
    public static boolean verboseLogging = false;

    @Override
    public ContainerRequest filter(ContainerRequest arg0) {
        startTime.set(System.currentTimeMillis());
        return arg0;
    }

    @Override
    public ContainerResponse filter(ContainerRequest request, ContainerResponse response) {
        System.out.println(System.currentTimeMillis() - startTime.get().longValue());
        StringBuilder sb = new StringBuilder();
        sb.append("User:").append((request.getUserPrincipal() == null ? "unknown" : request.getUserPrincipal().getName()));
        sb.append(" - Path:").append(request.getRequestUri().getPath());
        //...
    }

它会在开始和结束时拦截请求,以便您可以放入计时器或其他内容。

这适用于 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:

<init-param>
    <param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
    <param-value>blah.LoggingFilter</param-value>
</init-param>
<init-param>
    <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
    <param-value>blah.LoggingFilter</param-value>
</init-param>

and then this class:

public class LoggingFilter extends LoggingFilter implements ContainerRequestFilter {

    private static final ThreadLocal<Long> startTime = new ThreadLocal<Long>();
    public static boolean verboseLogging = false;

    @Override
    public ContainerRequest filter(ContainerRequest arg0) {
        startTime.set(System.currentTimeMillis());
        return arg0;
    }

    @Override
    public ContainerResponse filter(ContainerRequest request, ContainerResponse response) {
        System.out.println(System.currentTimeMillis() - startTime.get().longValue());
        StringBuilder sb = new StringBuilder();
        sb.append("User:").append((request.getUserPrincipal() == null ? "unknown" : request.getUserPrincipal().getName()));
        sb.append(" - Path:").append(request.getRequestUri().getPath());
        //...
    }

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.

失去的东西太少 2024-10-13 02:25:29

对于服务器部分,我们使用 Jersey Specific 类来执行如下操作: ContainerResponseFilter

签名是:

public ContainerResponse filter(ContainerRequest request, ContainerResponse response)

然后您可以执行如下调用:

Object entity  = response.getEntity();
    ... your logic here ...
return response;

这有帮助吗?..

For the server part we use a Jersey Specific class to do something like this: ContainerResponseFilter

The signature is:

public ContainerResponse filter(ContainerRequest request, ContainerResponse response)

then you can do calls like:

Object entity  = response.getEntity();
    ... your logic here ...
return response;

Can this be of some help ?..

伴随着你 2024-10-13 02:25:29

你看过球衣ClientFilter 类?

我们目前正在使用它来拦截和执行 API 版本控制等。有内置的日志过滤器 - 因此您可以查看它们的代码以了解要编写的内容。

签名是:

public ClientResponse handle(final ClientRequest cr) throws ClientHandlerException...

所以你可以从做类似的事情开始:

....
cr.getHeaders()
....
return getNext().handle(cr);

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:

public ClientResponse handle(final ClientRequest cr) throws ClientHandlerException...

So you can start by doing stuff like:

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