我正在使用 Spring MVC 并使用方面来通知我的 Controller 。我遇到了一个问题: Controller 返回一个用 @ResponseBody 类型注释的值。您如何找到应用于返回类型的注释?
@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;
}
建议的方法:
@RequestMapping("/Test")
public @ResponseBody String doTest(){
return "Test";
}
不幸的是,这两种方法似乎都没有达到预期的效果。我可以检索关于返回对象类型的注释,但不能检索在返回时添加的对象。
最佳答案
@ResponseBody
注释注释了一个方法,所以我想您会想要获取方法注释。
我不是 spring 专家,但我看到的例子是这样的:
@RequestMapping("/Test")
@ResponseBody
public String doTest(){
return "Test";
}
关于java - 在Java中获取返回类型的注解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13671531/