创建一个类 实现 BaseInterceptor接口,如下图:

    用这个注解去声明拦截器 @MarsInterceptor(pattern = “*”)

    pattern 为拦截规则,全部拦截 配置 * 即可,否则的话,必须以 / 开头

    1. @MarsInterceptor(pattern = "*")
    2. public class LoginInters implements BaseInterceptor {
    3. /**
    4. * http接口执行前
    5. * @param httpRequest
    6. * @param httpResponse
    7. * @return
    8. */
    9. public Object startRequest(HttpRequest httpRequest, HttpResponse httpResponse) {
    10. System.out.println("开始了");
    11. return SUCCESS;
    12. }
    13. /**
    14. * http接口执行后
    15. * @param httpRequest
    16. * @param httpResponse
    17. * @param o http接口返回的数据
    18. * @return
    19. */
    20. public Object endRequest(HttpRequest httpRequest, HttpResponse httpResponse, Object o) {
    21. System.out.println(o);
    22. System.out.println("结束了");
    23. return "拦截器报错了";
    24. }
    25. }

    如果拦截器顺利放行的话,返回SUCCESS就好了,否则直接返回 错误提示信息。

    generated by haroopad