• 获取SpringMVC所有的rest接口及其对应函数信息


    package com.geostar.gfstack.operationcenter.core.cloud.action;
    
    import com.geostar.gfstack.operationcenter.core.cloud.model.RestModel;
    import com.google.gson.Gson;
    import org.springframework.beans.factory.BeanFactoryUtils;
    import org.springframework.stereotype.Component;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.context.WebApplicationContext;
    import org.springframework.web.context.request.RequestContextHolder;
    import org.springframework.web.context.request.ServletRequestAttributes;
    import org.springframework.web.method.HandlerMethod;
    import org.springframework.web.servlet.DispatcherServlet;
    import org.springframework.web.servlet.HandlerMapping;
    import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition;
    import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
    import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
    
    import java.lang.reflect.Method;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    
    @Controller
    @Component("urlAction")
    @RequestMapping("urlAction")
    public class UrlAction {
    
        @ResponseBody
        @RequestMapping("index")
        public void index() {
            List<RestModel> list = new ArrayList<RestModel>();
            Map<String, RestModel> map = new HashMap<String, RestModel>();
            List<String> urlList = new ArrayList<String>();
            WebApplicationContext wac = (WebApplicationContext) ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE);
            Map<String, HandlerMapping> requestMappings = BeanFactoryUtils.beansOfTypeIncludingAncestors(wac, HandlerMapping.class, true, false);
            for (HandlerMapping handlerMapping : requestMappings.values()) {
                if (handlerMapping instanceof RequestMappingHandlerMapping) {
                    RequestMappingHandlerMapping rmhm = (RequestMappingHandlerMapping) handlerMapping;
                    Map<RequestMappingInfo, HandlerMethod> handlerMethods = rmhm.getHandlerMethods();
                    for (RequestMappingInfo rmi : handlerMethods.keySet()) {
                        PatternsRequestCondition prc = rmi.getPatternsCondition();
                        Set<String> patterns = prc.getPatterns();
                        HandlerMethod handlerMethod = handlerMethods.get(rmi);
                        for (String url : patterns) {
                            Class<?> clazz = handlerMethod.getBeanType();
                            Method method = handlerMethod.getMethod();
                            RestModel restModel = new RestModel();
                            restModel.setUrl(url);
                            restModel.setClazz(clazz.toString());
                            restModel.setMethod(method.toString());
                            map.put(url, restModel);
                            urlList.add(url);
                        }
                    }
                }
            }
            String[] urls = new String[urlList.size()];
            urls = urlList.toArray(urls);
            Arrays.sort(urls);
            for (String url : urls) {
                list.add(map.get(url));
            }
            Gson gson = new Gson();
            System.out.println(gson.toJson(list));
        }
    
    }
  • 相关阅读:
    python之re模块
    python之shell
    python之zip打包
    shell 处理解析json
    shell 中的传参-字符串传参,dict传参
    xshell 快速复制粘贴设置
    openstack 块存储服务 cinder
    win10系统中安装java环境
    mac上安装pip
    解决 谷歌浏览器报欧呦 崩溃了
  • 原文地址:https://www.cnblogs.com/nihaorz/p/7825666.html
Copyright © 2020-2023  润新知