一、过滤器
过滤器主要有客户端请求过滤器->服务器请求过滤器->服务器响应过滤器->客户端响应过滤器。涉及的接口包括:ClientRequestFilter、ContainerRequestFilter、ContainerResponseFilter和ClientResponseFilter。该部分接口主要涉及filter方法对请求进行过滤。实例如下:
请求过滤器代码
package com.kingdee.filter;
import java.io.IOException;
import java.util.Date;
import javax.annotation.Priority;
import javax.ws.rs.Priorities;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
/**
* 设置jersey过滤器,当有多个过滤器时,通过优先级@Priority设置优先级,
* 整数值小的先处理,整数值大的后处理。Priorities.AUTHENTICATION = 1000
Priorities.AUTHORIZATION = 2000
Priorities.HEADER_DECORATOR = 3000
Priorities.ENTITY_CODER = 4000
Priorities.USER = 5000
* @author Administrator
*
*/
@Priority(Priorities.AUTHENTICATION)
public class MyContainerRequestFilter implements ContainerRequestFilter{
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
// TODO Auto-generated method stub
System.out.println("接收客户端请求进行过滤处理=========="+new Date());
}
}
响应过滤器代码
package com.kingdee.filter;
import java.io.IOException;
import java.util.Date;
import javax.annotation.Priority;
import javax.ws.rs.Priorities;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
/**
* 在jersey的服务请求结束后,会经过服务器响应过滤器,同时也会通过@Priority设置优先级。
*
* @author Administrator
*
*/
@Priority(Priorities.AUTHENTICATION)
public class MyContainerResponseFilter implements ContainerResponseFilter{
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
throws IOException {
// TODO Auto-generated method stub
System.out.println("接收客户端响应进行过滤处理=========="+new Date());
}
}
注册过滤器代码
package com.kingdee.config;
import javax.ws.rs.ApplicationPath;
import org.glassfish.jersey.server.ResourceConfig;
import org.springframework.stereotype.Component;
import com.kingdee.filter.MyContainerRequestFilter;
import com.kingdee.filter.MyContainerResponseFilter;
@Component
@ApplicationPath("/microblog")
public class MicroblogResourceConfig extends ResourceConfig{
public MicroblogResourceConfig() {
System.out.println("000000000000000000000");
packages("com.kingdee.controller.rest");
register(MyContainerRequestFilter.class);
register(MyContainerResponseFilter.class);
}
}
二、绑定机制
绑定是将过滤器或者拦截器只作用域部分接口或者类,而不是通过注册的方式进行全局绑定。可以通过名称绑定的方式将过滤器或者拦截器绑定的指定的方法或者类中。代码如下:
设置名称绑定注解代码
package com.kingdee.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.lang.model.element.Element;
import javax.ws.rs.NameBinding;
/**
* 设置自定义名称注解
* @author Administrator
*
*/
@NameBinding
@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(value=RetentionPolicy.RUNTIME)
public @interface MyBindingFilter {
}
将自定义注解绑定到过滤器和拦截器
package com.kingdee.filter;
import java.io.IOException;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import com.kingdee.annotation.MyBindingFilter;
@MyBindingFilter
public class MyBindingRequestFilter implements ContainerRequestFilter{
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
// TODO Auto-generated method stub
System.out.println("自定义请求注解=====================");
}
}
将过滤器和拦截器绑定到指定的接口中
/**
* @Consumes将参数以一定的格式进行接收
* @Produces将结果以一定的格式进行输出
*/
@MyBindingFilter
@Path("json")
@POST
@Consumes("application/json")
@Produces("application/json")
public User findMicroblogByName(User user){
User user1 = new User();
user1.setName(user.getName());
user1.setOrgLongName(user.getOrgLongName());
System.out.println("===========json========="+user.getName());
return user;
}
三、优先级
当存在多个拦截器或者过滤器时,会按照一定的逻辑顺序执行。逻辑顺序需要通过@Priority优先级注解进行设置
代码如下
package com.kingdee.filter;
import java.io.IOException;
import java.util.Date;
import javax.annotation.Priority;
import javax.ws.rs.Priorities;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
/**
* 设置jersey过滤器,当有多个过滤器时,通过优先级@Priority设置优先级,
* 整数值小的先处理,整数值大的后处理。Priorities.AUTHENTICATION = 1000
Priorities.AUTHORIZATION = 2000
Priorities.HEADER_DECORATOR = 3000
Priorities.ENTITY_CODER = 4000
Priorities.USER = 5000
* @author Administrator
*
*/
@Priority(Priorities.AUTHENTICATION)
public class MyContainerRequestFilter implements ContainerRequestFilter{
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
// TODO Auto-generated method stub
System.out.println("接收客户端请求进行过滤处理=========="+new Date());
}
}