Get annotations of return type in Java
- by Apropos
I'm using Spring MVC and am using aspects to advise my controllers. I'm running into one issue: controllers that return a value annotated with the @ResponseBody type. How are you able to find the annotations applied to the return type?
@Around("myPointcut()")
private Object checkAnnotations(ProceedingJoinPoint pjp) throws Throwable {
Object result = pjp.proceed();
Method method = ((MethodSignature)pjp.getSignature()).getMethod();
System.out.println("Checking return type annotations.");
for(Annotation annotation : method.getReturnType().getAnnotations()){
System.out.println(annotation.toString());
}
System.out.println("Checking annotations on returned object.");
for(Annotation annotation : result.getClass().getAnnotations()){
System.out.println(annotation.toString());
}
return result;
}
Unfortunately, neither of these methods seem to have the desired effect. I can retrieve annotations on the type of object being returned, but not the ones being added at return time.