如何使用Jersey的内部路由机制来提取类/方法引用?
我正在运行Jersey 1.8应用程序。 Jersey正在作为Servlet运行。
我需要编写一个给出普通请求/响应的servletfilter ,能够确定哪个REST资源/方法将响应请求并从注释中提取值。
例如,假设我有以下资源:
@Path("/foo") @MyAnnotation("hello") public class FooResource { @GET @Path("/bar") @MyOtherAnnotation("world") public Response bar(){ ... } }
当请求GET /foo/bar
进来时,我需要我的servletfilter能够在Jersey自己的servlet处理请求之前从MyAnnotation
和MyOtherAnnotation
提取值"hello"
和"world"
。
此filter逻辑应该能够适用于所有请求和注册的所有资源。
有没有办法访问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)) {
注册filter使用
bind(AuthFilter.class).to(ContainerRequestFilter.class).in(Singleton.class);