1
私は多くを検索しましたが、それほど有用なものは何も見つかりませんでした。Guiceのネストされたアノテーションメソッドのインターセプト
問題: 私が作成したカスタム注釈のように:私はDIのためのGuiceを使用しています
@MapExceptions(value = {
@MapException(sources = {IllegalArgumentException.class, RuntimeException.class}, destination = BadRequestException.class),
@MapException(sources = {RuntimeException.class}, destination = BadRequestException.class)
})
。
- 2つのメソッドインターセプタを記述する必要がありますか?実際の作業は@MapExceptionで完了しています
- もしそうなら、@MapExceptionインターセプタは@MapExceptionsインターセプタのメソッド呼び出しメソッドを呼び出すことができますか?私はコードを複製したくない。
- マイ@MapExceptionインターセプターは
パブリッククラスMapExceptionInterceptor私はバインディング現在
bindInterceptor(Matchers.any(), Matchers.annotatedWith(MapException.class), new MapExceptionInterceptor());
次使用していますMethodInterceptorの{
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
try {
return invocation.proceed();
} catch (Exception actualException) {
Method method = invocation.getMethod();
Annotation[] annotations = method.getDeclaredAnnotations();
for (Annotation annotation : annotations) {
if (annotation instanceof MapException) {
MapException mapException = (MapException) annotation;
Class<? extends Throwable> destinationClass = mapException.destination();
Class<? extends Throwable>[] sourceClasses = mapException.sources();
for (Class sourceExceptionClass : sourceClasses) {
if (actualException.getClass().isInstance(sourceExceptionClass)) {
Constructor ctr = destinationClass.getConstructor(String.class);
throw (Throwable) ctr.newInstance(actualException.getMessage());
}
}
}
}
throw actualException;
}
}
}
が、これは大丈夫です実装し、次のようになります。 ?それとも改善することができますか?
ありがとうございました!
これを解決するために使用したコードを提供できますか? – behinddwalls