package org.sxkj.common.utils;
|
|
import lombok.SneakyThrows;
|
import org.springframework.web.bind.annotation.*;
|
|
import java.lang.annotation.Annotation;
|
import java.lang.reflect.Method;
|
import java.util.Arrays;
|
import java.util.List;
|
|
|
public class PathScannerUtils {
|
|
|
@SneakyThrows
|
public static void scanPathForAnnotations(Class<?> clazz) {
|
// 获取类中所有声明的方法
|
Method[] methods = clazz.getDeclaredMethods();
|
|
// 遍历每个方法
|
for (Method method : methods) {
|
// 获取方法上的所有注解
|
Annotation[] annotations = method.getAnnotations();
|
|
// 如果方法上有注解,则打印注解信息
|
if (annotations.length > 0) {
|
for (Annotation annotation : annotations) {
|
if (isRequestMappingAnnotation(annotation)) {
|
Method methodz = annotation.getClass().getMethod("value");
|
Object value = methodz.invoke(annotation);
|
System.out.println(((String[]) value)[0]);
|
}
|
|
}
|
}
|
}
|
|
|
}
|
|
public static boolean isRequestMappingAnnotation(Annotation annotation) {
|
// 定义所有 HTTP 请求映射注解类型
|
List<Class<?>> requestMappingAnnotations = Arrays.asList(
|
GetMapping.class,
|
PostMapping.class,
|
PutMapping.class,
|
DeleteMapping.class,
|
PatchMapping.class,
|
RequestMapping.class
|
);
|
|
// 检查注解是否在集合中
|
return requestMappingAnnotations.contains(annotation.annotationType());
|
}
|
|
|
|
|
}
|