feat(stu.base.annotation): 添加获取请求方法注解的工具类- 新增 AnnotationHelper 类,提供从 HttpServletRequest 中获取处理当前请求的方法上的注解的功能
- 该工具类使用 Spring 的 AnnotationUtils 和 HandlerMethod 来实现注解的查找 - 可用于在拦截器或其他地方快速获取方法级别的注解信息
This commit is contained in:
parent
e58c672e6d
commit
fe169aa12f
32
src/main/java/stu/base/annotation/AnnotationHelper.java
Normal file
32
src/main/java/stu/base/annotation/AnnotationHelper.java
Normal file
@ -0,0 +1,32 @@
|
||||
package stu.base.annotation;
|
||||
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.web.method.HandlerMethod;
|
||||
import org.springframework.web.servlet.HandlerMapping;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class AnnotationHelper {
|
||||
|
||||
/**
|
||||
* 从 HttpServletRequest 中获取处理当前请求的方法上的注解
|
||||
*/
|
||||
public static <T extends Annotation> T getMethodAnnotation(
|
||||
HttpServletRequest request,
|
||||
Class<T> annotationClass
|
||||
) {
|
||||
// 从请求属性中获取 HandlerMethod
|
||||
HandlerMethod handlerMethod = (HandlerMethod) request.getAttribute(
|
||||
HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE
|
||||
);
|
||||
|
||||
if (handlerMethod != null) {
|
||||
Method method = handlerMethod.getMethod();
|
||||
// 获取方法上的注解(包括继承的注解)
|
||||
return AnnotationUtils.findAnnotation(method, annotationClass);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user