SpringBoot项目获取所有的接口请求信息

Hi ~ 小老弟开始转公众号啦,欢迎大家来指点迷津呀

 

前排注意:这里涉及到了swagger的,没用到的可以自行更改。

关于动态feign调用的,可以参考这里~   关于动态创建Feign Client的问题


主入口:

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMethod;
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.*;
public List<Map<String, Object>> getUrl() {
    List<Map<String, Object>> list = new ArrayList<>();
    RequestMappingHandlerMapping mapping = webApplicationContext.getBean(RequestMappingHandlerMapping.class);
    Map<RequestMappingInfo, HandlerMethod> handlerMethods = mapping.getHandlerMethods();
    for (Map.Entry<RequestMappingInfo, HandlerMethod> entry : handlerMethods.entrySet()) {
        // 这里去掉swagger的接口
        String className = entry.getValue().getMethod().getDeclaringClass().getName();
        if (ObjectUtils.isEmpty(className) || className.contains("SwaggerConfig")) {
            continue;
        }
        Map<String, Object> map = new HashMap<>(16);
        map.putAll(getSwaggerProperties(entry.getValue()));
        map.put("requestType", getRequestType(entry.getKey().getMethodsCondition()));
        map.put("requestServer", webApplicationContext.getEnvironment().getProperty("spring.application.name"));
        // 一个方法可能对应多个url
        PatternsRequestCondition patternsCondition = entry.getKey().getPatternsCondition();
        for (String url : patternsCondition.getPatterns()) {
            map.put("requestUrl", getRequestUrl(url));
            list.add(map);
        }
    }
    return list;
}
getSwaggerProperties:
private Map<String, Object> getSwaggerProperties(HandlerMethod handlerMethod) {
    Map<String, Object> map = new HashMap<>(16);
    // 完整接口信息:类名信息 + 接口方法信息
    String requestName = "";
    // 接口方法补充描述
    String remark = "";

    // 类名的备注信息
    Api api = handlerMethod.getMethod().getDeclaringClass().getAnnotation(Api.class);
    if (handlerMethod.getMethod().getDeclaringClass().isAnnotationPresent(Api.class) && Objects.nonNull(api)) {
        requestName = api.value();
    }
    // 接口方法名的备注信息
    ApiOperation apiOperation = handlerMethod.getMethodAnnotation(ApiOperation.class);
    if (handlerMethod.hasMethodAnnotation(ApiOperation.class) && Objects.nonNull(apiOperation)) {
        requestName = ObjectUtils.isEmpty(requestName) ? apiOperation.value() : requestName + "_" + apiOperation.value();
        remark = ObjectUtils.isEmpty(apiOperation.notes()) ? "" : apiOperation.notes();
    } else {
        requestName = ObjectUtils.isEmpty(requestName) ? handlerMethod.getMethod().getName() : requestName + "_" + handlerMethod.getMethod().getName();
    }
    map.put("requestName", requestName);
    map.put("remark", remark);
    return map;
}
getRequestType:
private String getRequestType(RequestMethodsRequestCondition methodsCondition) {
    // 如果多个,改为一个POST
    String requestType = null;
    int count = 0;
    for (RequestMethod requestMethod : methodsCondition.getMethods()) {
        requestType = requestMethod.toString();
        count++;
    }
    return requestType == null || count > 1 ? "POST" : requestType;
}
getRequestUrl:
private String getRequestUrl(String url) {
    String separator = "/";
    String separator2 = "//";
    String separator3 = "///";
    String contextPath = webApplicationContext.getEnvironment().getProperty("server.servlet.context-path");
    String requestUrl;
    if (!ObjectUtils.isEmpty(contextPath) && !separator.equals(contextPath)) {
        requestUrl = contextPath + url;
        if (requestUrl.contains(separator3) || requestUrl.contains(separator2)) {
            requestUrl = requestUrl.replace(separator3, separator);
            requestUrl = requestUrl.replace(separator2, separator);
        }
    } else {
        requestUrl = url;
    }
    return requestUrl;
}

测试执行结果:

{
      "requestName": "类名备注-测试方法1",
      "requestServer": "alya-test",
      "requestType": "GET",
      "requestUrl": "/test/{id}",
      "remark": "测试方法备注信息1"
}, {
      "requestName": "类名备注-测试方法2",
      "requestServer": "alya-test",
      "requestType": "POST",
      "requestUrl": "/test",
      "remark": "测试方法备注信息2"
}

 最后,关于动态feign调用的,可以参考这里~   关于动态创建Feign Client的问题

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值