获取springboot框架下所有URL清单

文章介绍了在SpringBoot项目中获取所有URL的两种方法。方法一是通过RequestMappingHandlerMapping获取HandlerMethod信息,解析URL和HTTP方法。方法二是遍历带有@RestController注解的Bean,检查其方法上的@RequestMapping等注解来获取URL和HTTP方法。
摘要由CSDN通过智能技术生成

整理试用了网上推荐的两种获取springboot框架下所有URL的方法,并稍作调整,直接上代码:

方法1:

package com.xxx.util;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition;
import org.springframework.web.servlet.mvc.condition.RequestMethodsRequestCondition;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class UrlCollector {
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    private class UrlInfo {

        private String url;
        private String method;
        private String desc;
    }

    @Autowired
    WebApplicationContext applicationContext;

    @RequestMapping(value = "v1/getAllUrl", method = RequestMethod.GET)
    public Object getAllUrl() {
        RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
        // 获取url与类和方法的对应信息
        Map<RequestMappingInfo, HandlerMethod> map = mapping.getHandlerMethods();


        List<UrlInfo> list = new ArrayList<UrlInfo>();
        for (Map.Entry<RequestMappingInfo, HandlerMethod> m : map.entrySet()) {
            UrlInfo urlInfo = new UrlInfo();
            RequestMappingInfo info = m.getKey();
            HandlerMethod method = m.getValue();
            PatternsRequestCondition p = info.getPatternsCondition();
            for (String url : p.getPatterns()) {
                urlInfo.setUrl(url);
            }
//            map1.put("className", method.getMethod().getDeclaringClass().getName()); // 类名
//            urlInfo.setMethod(method.getMethod().getName());                  // 方法名
            RequestMethodsRequestCondition methodsCondition = info.getMethodsCondition();
            for (RequestMethod requestMethod : methodsCondition.getMethods()) {
                urlInfo.setMethod(requestMethod.toString());
            }

            list.add(urlInfo);
        }

        return list;
    }
}

方法二:

package com.xxx.util;

import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.WebApplicationContext;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
public class UrlCollector2 {

//    @Value("${server.servlet.context-path}")  //如有
//    public String system_code;
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    private class UrlInfo {

        private String url;
        private String method;
        private String desc;
    }

    @Autowired
    WebApplicationContext applicationContext;
    @RequestMapping(value = "v2/getAllUrl", method = RequestMethod.GET)
    public Object getAllUrl() {
        Map<String, Object> beans = applicationContext.getBeansWithAnnotation(RestController.class);
        List<UrlInfo> list = new ArrayList<UrlInfo>();

        for (String beanName : beans.keySet()) {
            Object value = applicationContext.getBean(beanName);
            if (value == null) {
                continue;
            }

            Method[] functions = value.getClass().getMethods();
            for (Method func : functions) {
                UrlInfo urlInfo = new UrlInfo();
                //每个方法必定含有下面的注解中的其中一个
//                ApiOperation apiOperation = AnnotationUtils.findAnnotation(func, ApiOperation.class);   //测试时这里我会报错,原因未知
//                if (apiOperation != null) {
//                    urlInfo.setDesc(apiOperation.value());
//                }
                String url = "";
                String reqMethod = "";
                RequestMapping reqMapping = AnnotationUtils.findAnnotation(func, RequestMapping.class);
                PostMapping postMapping = AnnotationUtils.findAnnotation(func, PostMapping.class);
                GetMapping getMapping = AnnotationUtils.findAnnotation(func, GetMapping.class);
                PutMapping putMapping = AnnotationUtils.findAnnotation(func, PutMapping.class);
                DeleteMapping deleteMapping = AnnotationUtils.findAnnotation(func, DeleteMapping.class);
                if (postMapping != null) {
                    url = postMapping.value()[0];
                    reqMethod = "POST";
                } else if (getMapping != null) {
                    url = getMapping.value()[0];
                    reqMethod = "GET";
                } else if (putMapping != null) {
                    url = putMapping.value()[0];
                    reqMethod = "PUT";
                } else if (deleteMapping != null) {
                    url = deleteMapping.value()[0];
                    reqMethod = "DELETE";
                } else if (reqMapping != null) {  //mapping 顺序一定要在后面
                    url = reqMapping.value()[0];
                    RequestMethod[] reqMethods = reqMapping.method();
                    for (RequestMethod req : reqMethods) {
                        if(reqMethod.equals("")){
                            reqMethod = req.toString();
                        }
                        else{
                            reqMethod = reqMethod +";"+ req.toString();
                        }

                    }

                } else {
                    continue;
                }
                urlInfo.setUrl(url);
                urlInfo.setMethod(reqMethod);
                //url信息入库

                list.add(urlInfo);
            }

        }
        return list;
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值