• 获取spring web项目的所有url请求信息


    本文讲述获取当前项目的所有restful请求的详细信息,含url、接口描述、请求方式等信息,主要用到了swagger的相关特性,最终返回的数据格式如下:

    [{
        "className": "cn.miao.controller.InfoController",
        "classDesc": "InfoController",
        "methodName": "getTimeStamp",
        "methodDesc": "获取时间戳",
        "methodURL": "/getTimeStamp",
        "requestType": "GET"
      },
    ...
    ]

    具体代码如下:

    package cn.miao.controller;
    
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiOperation;
    import lombok.extern.slf4j.Slf4j;
    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.lang.annotation.Annotation;
    import java.util.ArrayList;
    import java.util.LinkedHashMap;
    import java.util.List;
    import java.util.Map;
    
    @RestController
    @Api(value = "TestControllerApi", description = "获取url")
    @RequestMapping("/get/api/demo")
    @Slf4j
    public class TestController {
        @Autowired
        WebApplicationContext applicationContext;
    
        @RequestMapping(value = "/getAllUrl", method = RequestMethod.GET)
        public List<Map<String, String>> getAllUrl() {
            List<Map<String, String>> resList = new ArrayList<>();
            RequestMappingHandlerMapping requestMappingHandlerMapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
            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]);
                        }
                    }
                }
                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);
                }
                RequestMethodsRequestCondition methodsRequestCondition = requestMappingInfo.getMethodsCondition();
                for (RequestMethod requestMethod : methodsRequestCondition.getMethods()) {
                    resultMap.put("requestType", requestMethod.toString());
                }
                resList.add(resultMap);
            }
            return resList;
        }
    }

  • 相关阅读:
    前端Ajax/JS/HTML+后端SpringMVC(二)
    前端Ajax/JS/HTML+后端SpringMVC(一)
    Redis 简介及应用
    项目中使用 MyBatis(二)
    L2d插件
    [转载] 栈内存和堆内存
    Hbase排错
    matplotlib中文乱码
    cocos2dx 一些好网站
    esclipe中接入SDK时引用另一个工程或Jar
  • 原文地址:https://www.cnblogs.com/miaoying/p/13528941.html
Copyright © 2020-2023  润新知