如何使用Jersey的内部路由机制来提取类/方法引用? [英] How to use Jersey's internal routing mechanism to extract a class/method reference?
问题描述
我正在运行Jersey 1.8应用程序。 Jersey作为Servlet运行。
I have a Jersey 1.8 application running. Jersey is running as a Servlet.
我需要写一个 servlet过滤器给出一个简单的请求/响应,能够找出哪个REST资源/方法将响应请求并从注释中提取值。
I need to write a servlet filter that given a plain request/response, is able to figure out which REST resource/method will respond to the request and extract values from annotations.
例如,假设我有以下资源:
For example, imagine I have the following resource:
@Path("/foo")
@MyAnnotation("hello")
public class FooResource {
@GET
@Path("/bar")
@MyOtherAnnotation("world")
public Response bar(){
...
}
}
当请求 GET / foo / bar
进来时,我需要我的servlet过滤器才能从<$ c $中提取值hello
和world
在Jersey自己的servlet处理请求之前,c> MyAnnotation 和 MyOtherAnnotation
。
When a request GET /foo/bar
comes in, I need my servlet filter to be able to extract the values "hello"
and "world"
from MyAnnotation
and MyOtherAnnotation
before Jersey's own servlet processes the request.
此过滤器逻辑应该能够适用于所有请求和注册的所有资源。
This filter logic should be able to work for all requests and all resources registered.
有没有办法访问Jersey的内部路由机制来获取一个类/方法参考,其中Jersey将发送请求?
Is there a way to access Jersey's internal routing mechanism to obtain a class/method reference where Jersey will dispatch the request?
我也接受其他建议,但最好不要试图通过自己阅读 @Path
注释来破解我自己的路由机制。
I'm open to other suggestions as well, but ideally nothing like trying to hack my own routing mechanism by reading the @Path
annotations myself.
推荐答案
@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);
这篇关于如何使用Jersey的内部路由机制来提取类/方法引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!