• 获取一个Java项目的所有接口信息


    1.建立一个实体类

    public class RequestToMethodItem
        {
            public String controllerName;
            public String methodName;
            public String requestType;
            public String requestUrl;
            public Class<?>[] methodParmaTypes;
    
            public RequestToMethodItem(String requestUrl, String requestType, String controllerName, String requestMethodName, 
    
        Class<?>[] methodParmaTypes)
            {
                this.requestUrl = requestUrl;
                this.requestType = requestType;
                this.controllerName = controllerName;
                this.methodName = requestMethodName;
                this.methodParmaTypes = methodParmaTypes;
    
            }
        }

    2.从springbean容器中获取所有的实例bean封装成list转化成json输出

      @RequestMapping(value = "/xxx", method = RequestMethod.GET)
        public ModelAndView index(HttpServletRequest request)
        {
            ServletContext servletContext = request.getSession().getServletContext();
            if (servletContext == null)
            {
                return null;
            }
            WebApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
    
            //请求url和处理方法的映射
            List<RequestToMethodItem> requestToMethodItemList = new ArrayList<RequestToMethodItem>();
            //获取所有的RequestMapping
            Map<String, HandlerMapping> allRequestMappings = BeanFactoryUtils.beansOfTypeIncludingAncestors(appContext, 
        HandlerMapping.class, true, false);
    
            for (HandlerMapping handlerMapping : allRequestMappings.values())
            {
                //本项目只需要RequestMappingHandlerMapping中的URL映射
                if (handlerMapping instanceof RequestMappingHandlerMapping)
                {
                    RequestMappingHandlerMapping requestMappingHandlerMapping = (RequestMappingHandlerMapping) handlerMapping;
                    Map<RequestMappingInfo, HandlerMethod> handlerMethods = requestMappingHandlerMapping.getHandlerMethods();
                    for (Map.Entry<RequestMappingInfo, HandlerMethod> requestMappingInfoHandlerMethodEntry : handlerMethods.entrySet())
                    {
                // 获取实体对象 RequestMappingInfo requestMappingInfo
    = requestMappingInfoHandlerMethodEntry.getKey(); HandlerMethod mappingInfoValue = requestMappingInfoHandlerMethodEntry.getValue();             
                // 获取请求方式GET/POST/PUT/DELETE(如果@RequestMapping中没有写具体的请求方式则获取不到哦) RequestMethodsRequestCondition methodCondition
    = requestMappingInfo.getMethodsCondition();             Iterator<String> it = methodCondition.getMethods().iterator();
                String requestType = it.hasNext()? it.next().name : "";
                       
                // 获取请求的URL
                        PatternsRequestCondition patternsCondition = requestMappingInfo.getPatternsCondition();
                Iterator<String> iterator = patternsCondition.getPatterns().iterator();
                String requestUrl = iterator.hasNext()? iterator.next() : ""; //String requestUrl
    = SetUtils.first(patternsCondition.getPatterns());
                //获取接口所在的类名,方法名,参数名 String controllerName
    = mappingInfoValue.getBeanType().toString(); String requestMethodName = mappingInfoValue.getMethod().getName(); Class<?>[] methodParamTypes = mappingInfoValue.getMethod().getParameterTypes(); RequestToMethodItem item = new RequestToMethodItem(requestUrl, requestType, controllerName, requestMethodName, methodParamTypes); requestToMethodItemList.add(item); } break; } }
         //转化成json打印输出至控制台
         String s=JSON.toJSONString(requestToMethodItemList);
         System.out.println("List:"+s);
    return new ModelAndView("index").addObject("MethodList", requestToMethodItemList); }

    3.结果

    {
        "controllerName": "class com.gy.abc.RPCService",
        "methodName": "readMessage",
        "methodParmaTypes": ["java.lang.Integer"],
        "requestType": "",//注意:如果注解中没有写明请求方式,则此处会为空
        "requestUrl": "/xxx/xxx/message"
    }

    备注:以上方法亲测可用,不足之处还望指点!

    以上内容参考:

    https://www.cnblogs.com/yuananyun/archive/2014/08/25/3934371.html

    https://www.cnblogs.com/zhang-cb/p/9890900.html

    如需详细了解RequestCondition请移步https://blog.csdn.net/andy_zhang2007/article/details/88913776

  • 相关阅读:
    数据结构与算法 -- 动态规划算法
    数据结构与算法 -- 回溯算法
    数据结构与算法 -- 图
    数据结构与算法无用随笔
    算法集锦
    基于Zookeeper实现多进程分布式锁
    自己动手写线程池
    maven配置国内阿里云镜像
    自己动手写java锁
    使用jconsole监控JVM内存
  • 原文地址:https://www.cnblogs.com/leifongta/p/12092272.html
Copyright © 2020-2023  润新知