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を処理するには
おかげ