获取项目中所有URL--获取swagger上展示的接口信息

链接.
@Autowired
WebApplicationContext applicationContext;

@RequestMapping(value = "/getAllURL", method = RequestMethod.POST)
public Object getAllURL() {
    List<Map<String, String>> resultList = new ArrayList<>();

    RequestMappingHandlerMapping requestMappingHandlerMapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
    // 获取url与类和方法的对应信息
    Map<RequestMappingInfo, HandlerMethod> map = requestMappingHandlerMapping.getHandlerMethods();

    for (Map.Entry<RequestMappingInfo, HandlerMethod> mappingInfoHandlerMethodEntry : map.entrySet()) {
        Map<String, String> resultMap = new LinkedHashMap<>();

        RequestMappingInfo requestMappingInfo = mappingInfoHandlerMethodEntry.getKey();
        HandlerMethod handlerMethod = mappingInfoHandlerMethodEntry.getValue();

        resultMap.put("className",handlerMethod.getMethod().getDeclaringClass().getName()); // 类名
        Annotation[] parentAnnotations = handlerMethod.getBeanType().getAnnotations();
        for (Annotation annotation : parentAnnotations) {
            if (annotation instanceof Api) {
                Api api = (Api) annotation;
                resultMap.put("classDesc",api.value());
            } else if (annotation instanceof RequestMapping) {
                RequestMapping requestMapping = (RequestMapping) annotation;
                if (null != requestMapping.value() && requestMapping.value().length > 0) {
                    resultMap.put("classURL",requestMapping.value()[0]);//类URL
                }
            }
        }
        resultMap.put("methodName", handlerMethod.getMethod().getName()); // 方法名
        Annotation[] annotations = handlerMethod.getMethod().getDeclaredAnnotations();
        if (annotations != null) {
            // 处理具体的方法信息
            for (Annotation annotation : annotations) {
                if (annotation instanceof ApiOperation) {
                    ApiOperation methodDesc = (ApiOperation) annotation;
                    String desc = methodDesc.value();
                    resultMap.put("methodDesc",desc);//接口描述
                }
            }
        }
        PatternsRequestCondition p = requestMappingInfo.getPatternsCondition();
        for (String url : p.getPatterns()) {
            resultMap.put("methodURL",url);//请求URL
        }
        RequestMethodsRequestCondition methodsCondition = requestMappingInfo.getMethodsCondition();
        for (RequestMethod requestMethod : methodsCondition.getMethods()) {
            resultMap.put("requestType",requestMethod.toString());//请求方式:POST/PUT/GET/DELETE
        }
        resultList.add(resultMap);
    }
    return JSONArray.fromObject(resultList);
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值