blog

Springブートアノテーション+インターセプター

カスタムアノテーションの実装 Target // アノテーションはメソッド上で使用されます。 @Retention() // アノテーションは実行時にも存在します。 p...

Apr 13, 2020 · 1 min. read
  • カスタムアノテーションの実装
@Target({ElementType.METHOD}) // アノテーションはメソッドで使われる
@Retention(RetentionPolicy.RUNTIME) // アノテーションは実行時にも存在する
public @interface MyAnnotation {}
  • カスタムインターセプター
public class MyInterceptor extends HandlerInterceptorAdapter {
 @Override
 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
 if(!(handler instanceof HandlerMethod)) {
 return true;
 }
 HandlerMethod handlerMethod = (HandlerMethod) handler;
 Method method = handlerMethod.getMethod();
 MyAnnotation annotation = method.getAnnotation(MyAnnotation.class);
 if(annotation != null) {
 // 適宜処理する
 }
 return true;
 }
}
  • 配置
@Configuration
public class WebConfiguration extends WebMvcConfigurationSupport {
 @Bean
 public MyInterceptor myInterceptor() {
 return new MyInterceptor();
 }
 /**
 * インターセプターを追加する
 * @param registry
 */
 @Override
 public void addInterceptors(InterceptorRegistry registry) {
 registry.addInterceptor(myInterceptor()).addPathPatterns("/**");
 super.addInterceptors(registry);
 }
}
Read next

Pure js export excel

ステップ1: js-export-excelプラグインのインストール npm install js-export-excel または yarn add js-export-excel ステップ2: プロジェクトにインポートします インポート

Apr 12, 2020 · 1 min read