一个人怎么拦截泽西生命周期中的请求? [英] How does one intercept a request during the Jersey lifecycle?

查看:137
本文介绍了一个人怎么拦截泽西生命周期中的请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用球衣一年的大部分时间现在刚刚偶然发现了一个问题,我无法找到答案:你怎么拦截(或挂接到)泽西请求生命周期

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?

在理想情况下,我会能够执行一些自定义筛选/确认/时之间拒绝接受容器从网络的要求和时间我的处理方法被调用。奖励积分,如果有,以滤除子路径拦截一个简单的方法(例如有下/任何1拦截器,另一个是在/用户/任何东西,等等)。

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.).

谢谢!

编辑:要成为一个更清楚一点,这里的总体思路是要能写一些code,会为许多API调用运行,而无需显式调用每个处理方法code。这将减少额外的code和消除需要绕过的请求上下文。

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.

推荐答案

我已经找到了答案。

首先,创建一个实现ContainerRequestFilter一类。界面指定以下方法,其中,滤波发生。该ContainerRequest对象包含关于当前请求的信息。

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);

在此之后,包括在servlet配置以下XML在web.xml中

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>

来源:

<一个href=\"http://jersey.576304.n2.nabble.com/ContainerRequestFilter-and-Resources-td4419975.html\">http://jersey.576304.n2.nabble.com/ContainerRequestFilter-and-Resources-td4419975.html
<一href=\"http://markmail.org/message/p7yxygz4wpakqno5\">http://markmail.org/message/p7yxygz4wpakqno5

这篇关于一个人怎么拦截泽西生命周期中的请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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