吉安感知网项目-后端
xiebin
2026-01-06 d207a86cdf1ab52ef8cb7cd83bad8fceab8038cf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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());
    }
 
 
 
 
}