「Spring」HandlerMethodクラスでメソット名、クラス名とアノテーション判定のサンプル

2020年11月5日

HandlerMethod説明
メソッドと Bean で構成されるハンドラーメソッドに関する情報をカプセル化します。
メソッドのパラメーター、メソッドの戻り値、メソッドのアノテーションなどへの便利なアクセスを提供します。
サンプルコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response,
Object handler) throws Exception {
HandlerMethod hdrMehod = (HandlerMethod)handler;
//method name メソット名
String methodName= hdrMethod.getMethod().getName();
log.debug("method is invoked:" + methodName);
//class name クラス名
String className= hdrMethod.getBean().getClass().getSimpleName();
log.debug("class name :" + className);
//AuthLevel annotaion アノテーション
boolean result;
result=hdrMethod.getMethod().isAnnotationPresent(AuthLevel.class);
}
@Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { HandlerMethod hdrMehod = (HandlerMethod)handler; //method name メソット名 String methodName= hdrMethod.getMethod().getName(); log.debug("method is invoked:" + methodName); //class name クラス名 String className= hdrMethod.getBean().getClass().getSimpleName(); log.debug("class name :" + className); //AuthLevel annotaion アノテーション boolean result; result=hdrMethod.getMethod().isAnnotationPresent(AuthLevel.class); }
  @Override
  public boolean preHandle(HttpServletRequest request,
                    HttpServletResponse response,
                    Object handler) throws Exception {
                              
                        HandlerMethod hdrMehod = (HandlerMethod)handler;
                        //method name メソット名
                        String methodName= hdrMethod.getMethod().getName();
                        log.debug("method is invoked:" + methodName);
                        
                        //class name クラス名
                        String className= hdrMethod.getBean().getClass().getSimpleName();
                        log.debug("class name :" + className);
                        
                        //AuthLevel annotaion アノテーション
                        boolean result;
                        result=hdrMethod.getMethod().isAnnotationPresent(AuthLevel.class);
                        

}

 

SpringMVC

Posted by arkgame