cur*_*ind 10 java web-services jersey filter
我用jersey来创建webservices.我已经创建了请求过滤器ContainerRequestFilter.我只针对某些URI问题浏览了 泽西请求过滤器,但我想排除某些网址的过滤器.
@Provider
public class AuthFilter implements ContainerRequestFilter{
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
// business logic
}
}
Run Code Online (Sandbox Code Playgroud)
cas*_*lin 24
您可以考虑使用名称绑定过滤器来选择过滤器将绑定到的端点,而不是从全局过滤器中排除URI .
有关名称绑定过滤器的一些示例,请查看此答案.
如果您对全局过滤器方法仍然满意,可以考虑使用该UriInfo接口来获取有关所请求URI的详细信息.使用以下方法之一获取以下实例UriInfo:
使用@Context注释:
@Provider
public class AuthFilter implements ContainerRequestFilter {
@Context
private UriInfo info;
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
...
}
}
Run Code Online (Sandbox Code Playgroud)从以下方面获取ContainerRequestContext:
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
UriInfo info = requestContext.getUriInfo();
...
}
Run Code Online (Sandbox Code Playgroud)获得UriInfo实例后,您将可以访问一些可能有用的方法:
getAbsolutePath():获取请求的绝对路径.getBaseUri():获取应用程序的基本URI.getMatchedResources():获取当前匹配的资源类实例的只读列表.getMatchedURIs():获取匹配资源的URI的只读列表.getPath():以字符串形式获取当前请求相对于基URI的路径.getPathSegments():获取当前请求相对于基URI的路径作为列表PathSegment.getRequestUri():获取绝对请求URI,包括任何查询参数.relativize(URI):相对于当前请求URI重新激活URI.resolve(URI):解析相对于应用程序的基URI的相对URI.有关更多详细信息,请查看UriInfo文档.
如果请求的URI与您要应用过滤器的URI不匹配,只需使用return指令:
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
UriInfo info = requestContext.getUriInfo();
if (!info.getPath().contains("secured")) {
return;
}
}
Run Code Online (Sandbox Code Playgroud)
另一种方法是动态绑定.它允许您以动态方式为资源方法分配过滤器和拦截器.上面提到的名称绑定使用静态方法,对绑定的更改需要更改源代码和重新编译.使用动态绑定,您可以实现在应用程序初始化时定义绑定的代码.
从Jersey文档中提取的以下示例显示了如何实现动态绑定:
@Path("helloworld")
public class HelloWorldResource {
@GET
@Produces("text/plain")
public String getHello() {
return "Hello World!";
}
@GET
@Path("too-much-data")
public String getVeryLongString() {
String str = ... // very long string
return str;
}
}
Run Code Online (Sandbox Code Playgroud)
// This dynamic binding provider registers GZIPWriterInterceptor
// only for HelloWorldResource and methods that contain
// "VeryLongString" in their name. It will be executed during
// application initialization phase.
public class CompressionDynamicBinding implements DynamicFeature {
@Override
public void configure(ResourceInfo resourceInfo, FeatureContext context) {
if (HelloWorldResource.class.equals(resourceInfo.getResourceClass())
&& resourceInfo.getResourceMethod().getName().contains("VeryLongString")) {
context.register(GZIPWriterInterceptor.class);
}
}
}
Run Code Online (Sandbox Code Playgroud)
绑定是使用实现DynamicFeature接口的提供程序完成的.接口定义了一个configure带有两个参数的方法,ResourceInfo和FeatureContext.
ResourceInfo包含有关可以执行绑定的资源和方法的信息.configure方法将针对应用程序中定义的每个资源方法执行一次.在上面的示例中,提供程序将执行两次,一次用于getHello()方法,一次用于getVeryLongString()(一次resourceInfo将包含有关getHello()方法的信息,一旦它将指向getVeryLongString()).
如果动态绑定提供程序想要为实际资源方法注册任何提供程序,它将使用提供的FeatureContext扩展JAX-RS可配置API 来执行该操作.可以使用所有用于注册过滤器或拦截器类或实例的方法.这种动态注册的过滤器或拦截器将仅绑定到实际的资源方法.在上面的示例中,GZIPWriterInterceptor将仅绑定到getVeryLongString()将导致仅针对此方法而不针对该方法压缩数据的方法getHello().
请注意,使用动态绑定注册的过滤器和拦截器只是为资源方法运行的其他过滤器.如果有任何名称绑定提供程序或全局提供程序,它们仍将被执行.
有关更多详细信息,请查看有关过滤器和拦截器的Jersey文档.
| 归档时间: |
|
| 查看次数: |
7857 次 |
| 最近记录: |