美文网首页Spring-webmvc
在Controller方法上添加自定义注解, 并解析自定义注解

在Controller方法上添加自定义注解, 并解析自定义注解

作者: xzz4632 | 来源:发表于2019-07-15 11:10 被阅读0次
1. 定义自定义注解
@Retention(RUNTIME) 
@Target(METHOD)
public @interface RequiredToken {
    // 可定义字段
}
2. 使用注解

如在某个controller方法上使用了注解. 如我们在拦截器中要拦截使用了自定义注解的方法.

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
        throws Exception {
        HandlerMethod handlerMethod = (HandlerMethod)handler;

        Method method = handlerMethod.getMethod();
        RequiredToken rt = method.getAnnotation(RequiredToken.class);
        if (rt == null) {
            return true;
        }
        return true;
    }

注: 通过HandlerMethod对象获取相关注解.

相关文章

网友评论

    本文标题:在Controller方法上添加自定义注解, 并解析自定义注解

    本文链接:https://www.haomeiwen.com/subject/zfwgkctx.html