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

    执行结果:

  • 相关阅读:
    Webform Session、Cookies传值,跳转页面方式
    webform 光棒效果,删除操作弹出确定取消窗口
    webform Repeater、地址栏传值、Response
    WebForm 控件
    WebForm 页面传值
    WebForm——IIS服务器、开发方式和简单基础
    身份证件号的验证与更改
    历届试题 带分数
    算法提高 学霸的迷宫
    算法提高 最大乘积
  • 原文地址:https://www.cnblogs.com/xwxz/p/13998843.html
Copyright © 2020-2023  润新知