1. 自定义接口

// 什么都不用写,就定义一个空接口
public interface MyMark {

}

2. Controller接口类实现自定义接口

@RestController
public class DayController implements MyMark {

    @RequestMapping("/day1")
    public String get1(){
        return "day1";
    }

    @RequestMapping("/day2")
    public String get2(){
        return "day2";
    }

}
@RestController
@RequestMapping("/month")
public class MonthController implements MyMark {

    @RequestMapping
    public String get1(){
        return "No1";
    }

    @RequestMapping("/No2")
    public String get2(){
        return "No2";
    }

}
@RestController
@RequestMapping
public class YearController implements MyMark {

    @RequestMapping
    public String get1(){
        return "No1";
    }

    @RequestMapping("/No2")
    public String get2(){
        return "No2";
    }
}

3. 编写配置类自动获取所有接口路径

@Configuration
public class MenuSynchroConfig {

    @Autowired
    WebApplicationContext applicationContext;
    private static Map<String, List<String>> urls = new HashMap<>();

    // 服务启动后执行
    @PostConstruct
    public void initialize() {
        list();
    }

    public void list(){
        // 获取所有实现自定义接口的类
        Map<String, AuthStat> beansOfType = applicationContext.getBeansOfType(AuthStat.class);
        for (String key : beansOfType.keySet()) {
            // 获取类内的所有方法
            Method[] methods = beansOfType.get(key).getClass().getMethods();
            // 获取类的注解
            RequestMapping requestMapping = beansOfType.get(key).getClass().getAnnotation(RequestMapping.class);
            // 判断注解的value值是否为空
            if (requestMapping != null && requestMapping.value().length != 0) {
                // 获取注解的value值数组
                String[] value1 = requestMapping.value();
                // 循环值数组
                for (String s1 : value1) {
                    // 循环方法数组
                    for (Method method : methods) {
                        // 获取方法的注解
                        RequestMapping annotation = method.getAnnotation(RequestMapping.class);
                        // 注解不为空
                        if (annotation != null) {
                            // 获取注解的value值数组
                            String[] value = annotation.value();
                            // 循环值数组
                            for (String s : value) {
                                // 拼接类注解value值与方法注解value值
                                urls.add(s1 + s);
                            }
                        }
                    }
                }
            } else {
                for (Method method : methods) {
                    RequestMapping annotation = method.getAnnotation(RequestMapping.class);
                    if(annotation!=null){
                        String[] value = annotation.value();
                        for (String s : value) {
                            urls.add(s);
                        }
                    }
                }
            }
        }
        urls.forEach(System.out::println);
    }
}
## 运行结果
/day1
/day2
/month/No2
/No2

 

----------------------------------- 作者:怒吼的萝卜 -----------------------------------