「Spring」findAnnotationメソッドでアノテーションが付与されるかどうかチェックする
書式
1.public abstract class AnnotationUtils extends Object
アノテーションの操作、メタアノテーションの処理、ブリッジメソッド(コンパイラーがジェネリクス宣言用に生成)、およびスーパーメソッド(オプションのアノテーション継承)を処理するための一般的なユーティリティメソッド。
2.findAnnotation(MethodSE method, @Nullable ClassSE<A> annotationType)
method :アノテーションを探すメソッド
annotationType:検索するアノテーション型
戻り値: 最初に一致したアノテーション。見つからない場合は null
使用例
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
HandlerMethod handlerMethod = (HandlerMethod) handler;
Method method = handlerMethod.getMethod();
UserAuth annotation = AnnotationUtils.findAnnotation(method, UserAuth.class);
if (annotation != null) {
処理コード
return true;
}
}