创建一个类 实现 BaseInterceptor接口,如下图:
用这个注解去声明拦截器 @MarsInterceptor(pattern = “*”)
pattern 为拦截规则,全部拦截 配置 * 即可,否则的话,必须以 / 开头
@MarsInterceptor(pattern = "*")
public class LoginInters implements BaseInterceptor {
/**
* http接口执行前
* @param httpRequest
* @param httpResponse
* @return
*/
public Object startRequest(HttpRequest httpRequest, HttpResponse httpResponse) {
System.out.println("开始了");
return SUCCESS;
}
/**
* http接口执行后
* @param httpRequest
* @param httpResponse
* @param o http接口返回的数据
* @return
*/
public Object endRequest(HttpRequest httpRequest, HttpResponse httpResponse, Object o) {
System.out.println(o);
System.out.println("结束了");
return "拦截器报错了";
}
}
如果拦截器顺利放行的话,返回SUCCESS就好了,否则直接返回 错误提示信息。
generated by haroopad