zoukankan      html  css  js  c++  java
  • 获取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));
        }
    
    }
  • 相关阅读:
    BZOJ_3170_[Tjoi2013]松鼠聚会_切比雪夫距离
    BZOJ_3343_教主的魔法_分块+二分查找
    吴裕雄--天生自然HADOOP操作实验学习笔记:使用hive操作hbase
    吴裕雄--天生自然云计算安全策略:云计算安全
    吴裕雄--天生自然HADOOP操作实验学习笔记:hbase的shell应用v2.0
    吴裕雄--天生自然HADOOP操作实验学习笔记:hive DDL
    吴裕雄--天生自然HADOOP操作实验学习笔记:mapreduce和yarn命令
    吴裕雄--天生自然HADOOP操作实验学习笔记:hdfs简单的shell命令
    吴裕雄--天生自然HADOOP操作实验学习笔记:mapreduce代码编程
    吴裕雄--天生自然HADOOP操作实验学习笔记:分布式及RPC通信简介
  • 原文地址:https://www.cnblogs.com/nihaorz/p/7825666.html
Copyright © 2011-2022 走看看