Get annotations of return type in Java

Posted by Apropos on Stack Overflow See other posts from Stack Overflow or by Apropos
Published on 2012-12-02T17:01:29Z Indexed on 2012/12/02 17:03 UTC
Read the original article Hit count: 208

Filed under:
|
|
|

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.

© Stack Overflow or respective owner

Related posts about java

Related posts about methods