我正在运行 Jersey 1.8 应用程序。 Jersey 作为 Servlet 运行。
我需要写一个小服务程序过滤器给定一个简单的请求/响应,能够确定哪个 REST 资源/方法将响应该请求并从注释中提取值。
例如,假设我有以下资源:
@Path("/foo")
@MyAnnotation("hello")
public class FooResource {
@GET
@Path("/bar")
@MyOtherAnnotation("world")
public Response bar(){
...
}
}
当一个请求GET /foo/bar
进来,我需要我的 servlet 过滤器能够提取值"hello"
and "world"
from MyAnnotation
and MyOtherAnnotation
在 Jersey 自己的 servlet 处理请求之前。
此过滤器逻辑应该能够适用于所有请求和注册的所有资源。
有没有办法访问 Jersey 的内部路由机制来获取 Jersey 将调度请求的类/方法引用?
我也愿意接受其他建议,但理想情况下没有什么比尝试通过阅读来破解我自己的路由机制更好的了。@Path
自己注释一下。
@Provider
@Priority(Priorities.AUTHORIZATION)
public class MyFilter implements ContainerRequestFilter
@Context // request scoped proxy
private ResourceInfo resourceInfo;
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
if (resourceInfo.getResourceClass().isAnnotationPresent(MyAnnotationion.class) ||
resourceInfo.getResourceMethod().isAnnotationPresent(MyOtherAnnotation.class)) {
注册过滤器使用
bind(AuthFilter.class).to(ContainerRequestFilter.class).in(Singleton.class);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)