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信息入库
    
    
                }
    
            }
    
        }
    }
    
    

    执行结果:

  • 相关阅读:
    RAC连接时的2种方式Connect Time Failver和taf
    ElasticSearch Root身份运行
    sql in按照指定顺序排序
    JAVA字符串格式化-String.format()的使用
    java.lang.IllegalArgumentException: An invalid domain [.test.com] was specified for this cookie
    全文搜索引擎 Elasticsearch 入门教程
    监控页面后退前进,浏览器文档加载事件之pageshow、pagehide
    ios 上浏览器返回上一页不会刷新页面问题,页面初始化的方法不执行
    SQL之case when then用法
    MySQL表的四种分区类型
  • 原文地址:https://www.cnblogs.com/xwxz/p/13998843.html
Copyright © 2011-2022 走看看