zoukankan      html  css  js  c++  java
  • springboot获取当前项目所有URL和URL描述

    package com.tusdao.tbs.base.conf;
    
    import io.swagger.annotations.ApiOperation;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    import org.springframework.core.annotation.AnnotationUtils;
    import org.springframework.stereotype.Component;
    import org.springframework.web.bind.annotation.*;
    
    import java.lang.reflect.Method;
    import java.util.Map;
    
    @Slf4j
    @Component
    public class GetUrlLoader implements ApplicationContextAware {
    
    //    @Value("${app.id}")
    //    public String system_code;
    
        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            Map<String, Object> beans = applicationContext.getBeansWithAnnotation(RestController.class);
            for (String beanName : beans.keySet()) {
                Object value = applicationContext.getBean(beanName);
                if (value == null) {
                    continue;
                }
                RequestMapping requestMapping = AnnotationUtils.findAnnotation(value.getClass(), RequestMapping.class);
                if (requestMapping == null) {
                    continue;
                }
                String path = requestMapping.value()[0];
                log.info("path: " + path);
                Method[] methods = value.getClass().getMethods();
                for (Method method : methods) {
                    //每个方法必定含有下面的注解中的其中一个
                    ApiOperation apiOperation = AnnotationUtils.findAnnotation(method, ApiOperation.class);
                    String url = "";
                    String desc = "";
                    if (apiOperation != null) {
                        desc = apiOperation.value();
                    }
                    RequestMapping mapping = AnnotationUtils.findAnnotation(method, RequestMapping.class);
                    PostMapping postMapping = AnnotationUtils.findAnnotation(method, PostMapping.class);
                    GetMapping getMapping = AnnotationUtils.findAnnotation(method, GetMapping.class);
                    PutMapping putMapping = AnnotationUtils.findAnnotation(method, PutMapping.class);
                    DeleteMapping deleteMapping = AnnotationUtils.findAnnotation(method, DeleteMapping.class);
                    if (postMapping != null) {
                        url = path + postMapping.value()[0];
                    } else if (getMapping != null) {
                        url = path + getMapping.value()[0];
                    } else if (putMapping != null) {
                        url = path + putMapping.value()[0];
                    } else if (deleteMapping != null) {
                        url = path + deleteMapping.value()[0];
                    } else if (mapping != null) {  //mapping 顺序一定要在后面
                        url = path + mapping.value()[0];
                    } else {
                        continue;
                    }
                    //log.info("url : {}  , desc : {}, system_code:{}", url, desc, system_code);
                    log.info("url : {}  , desc : {}", url, desc);
                    //url信息入库
    
    
                }
    
            }
    
        }
    }
    
    

    执行结果:

  • 相关阅读:
    页面内容[置顶] 采用Div+Css布局——牛腩
    区域函数[置顶] linux 3.4.10 内核内存管理源代码分析5:伙伴系统初始化
    安装应用android批量安装APK
    选择版本Win7系统VS2010下搭建qt开发环境
    字体格式The format of Oracle tnsnames.ora file
    程序执行vhdl中延时器的编写
    概率链接nbu 2416 奇怪的散步
    中国主题ASP.Net课堂实验4
    要求终点HDU1010:Tempter of the Bone
    解决方案编程苦B和二B程序员别忘了养生
  • 原文地址:https://www.cnblogs.com/xwxz/p/13998843.html
Copyright © 2011-2022 走看看