2016-08-25 6 views
2

Springの残りのAPI用のカスタム注釈を作成しようとしています。BeanPostProcessorを使用したSpringのカスタム注釈

@SpringBootApplication 
@ComponentScan(basePackageClasses = {ServiceController.class, CustomAnnotatorProcessor.class}) 
public class ServiceApp { 
    public static void main(String[] args) {    
     SpringApplication.run(ServiceApp.class, args); 
    } 
} 

RestController - -

@RestController 
public class ServiceController { 

    @RequestMapping(method = RequestMethod.GET, value="/service/v1/version") 
    @ApiResponses(value = { 
      @ApiResponse(code = 200, message = "Success", response = String.class), 
      @ApiResponse(code = 401, message = "Unauthorized"), 
      @ApiResponse(code = 403, message = "Forbidden"), 
      @ApiResponse(code = 404, message = "Not Found"), 
      @ApiResponse(code = 500, message = "Failure")}) 
    @CustomAnnotation() 
    public String getVersion() { 
     return "success"; 
    } 
} 

カスタム注釈 -

@Retention(RetentionPolicy.RUNTIME) 
@Target({ElementType.METHOD}) 
@Documented 
public @interface CustomAnnotation { 

} 
Iカスタムアノテーションを作成するための新しいです、私は

春ブーツアプリの下のコードスニペットを与えています

注釈プロセッサー -

@Component パブリッククラスCustomAnnotatorProcessorは{

private ConfigurableListableBeanFactory configurableBeanFactory; 

@Autowired 
public CustomAnnotatorProcessor(ConfigurableListableBeanFactory beanFactory) { 
    this.configurableBeanFactory = beanFactory; 
} 

@Override 
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { 
    return bean; 
} 

@Override 
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { 
    MethodCallback methodCallback = new CustomAnnotationMethodCallback(configurableBeanFactory, bean); 
    ReflectionUtils.doWithMethods(bean.getClass(), methodCallback); 
    return bean; 
} 

メソッドのコールバックBeanPostProcessorを実装 -

public class CustomAnnotationMethodCallback implements MethodCallback{ 
    @Override 
    public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException { 
     if (method.isAnnotationPresent(CustomAnnotation.class)) { 
      System.out.println("doWith is getting called for CustomAnnotationMethodCallback"); 
      ReflectionUtils.makeAccessible(method);  
      //DO VALIDATION WHETHER A SPECIFIC HEADER IS PRESENT IN THE GIVEN REQUEST 
      return; 
     }  
    } 

} 

私はBeanPostProcessorを実装するクラスでカスタムアノテーションを処理しようとしていますが、私は問題を持っている

Issue_1:コールバックが1回呼び出されていますが、すべてのリクエストに対して検証を適用できません。 hatが/ service/v1/version APIに来ています。私はすべてのリクエストで検証する必要がありますが、私たちの設計/アプローチが正しいかどうかは、この問題を解決する方法ではない場合は、別のアプローチを提案してください。

Issue_2:ヘッダー)を@customAnnotationに追加するには、どうすればよいですか?

カスタム注釈@validateAuthenticationを処理するには

おかげ

答えて

0

をさらに詳細が必要な場合は私に知らせてください、私たちはHandlerInterceptorAdapterを拡張するIntercepterクラスを作成しました。

preHandle(HttpServletRequestリクエスト、HttpServletResponseレスポンス、オブジェクトハンドラ)メソッドにリクエストヘッダーの検証を実装しました。

0

Issue_1:コールバックは一度呼び出されていますが、/service/v1/version APIに送信されるリクエストごとに検証を適用することができません。私は、リクエストごとに検証する必要がどのようにこの問題を解決するためにそうならば、正しい私たちのデザイン/アプローチであり、別のアプローチを提案してくださいいない場合

Issue_2:私は(単独で、完全なリクエストオブジェクトを渡す必要がある場合ヘッダー)を@customAnnotationに変更するには、どうすればよいですか?

Annotation以外のSpring AOPやInterceptorを使用してください。

関連する問題