resteasy拦截器,拦截rest请求,修改response header内容

只拦截GET请求,并修改header中的内容反馈给客户端

[java]  view plain copy
  1. import java.io.IOException;  
  2. import java.lang.reflect.Method;  
  3.   
  4. import javax.ws.rs.GET;  
  5. import javax.ws.rs.WebApplicationException;  
  6. import javax.ws.rs.ext.Provider;  
  7.   
  8. import org.jboss.resteasy.annotations.interception.ServerInterceptor;  
  9. import org.jboss.resteasy.spi.interception.AcceptedByMethod;  
  10. import org.jboss.resteasy.spi.interception.MessageBodyWriterContext;  
  11. import org.jboss.resteasy.spi.interception.MessageBodyWriterInterceptor;  
  12.   
  13. @Provider  
  14. @ServerInterceptor  
  15. public class MyHeaderDecorator implements MessageBodyWriterInterceptor,  
  16.         AcceptedByMethod {  
  17.   
  18.     public boolean accept(Class declaring, Method method) {  
  19.         return method.isAnnotationPresent(GET.class);  
  20.     }  
  21.   
  22.     public void write(MessageBodyWriterContext context) throws IOException,  
  23.             WebApplicationException {  
  24.   
  25.         context.getHeaders().add("My-Header""custom");  
  26.         context.proceed();  
  27.     }  
  28. }  

参考http://docs.jboss.org/resteasy/docs/1.1.GA/userguide/html/Interceptors.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值