zoukankan      html  css  js  c++  java
  • RequestMapper

    @RequestMapping(value = "/v1/getAllUrl", method = RequestMethod.POST)
      public Object getAllUrl() {
          RequestMappingHandlerMapping mapping = SpringContextHolder.getBean(RequestMappingHandlerMapping.class);
          // 获取url与类和方法的对应信息
          Map<RequestMappingInfo, HandlerMethod> map = mapping.getHandlerMethods();
    
    List<String> urlList = new ArrayList<>();
    for (RequestMappingInfo info : map.keySet()) {
       // 获取url的Set集合,一个方法可能对应多个url
       Set<String> patterns = info.getPatternsCondition().getPatterns();
    
       for (String url : patterns) {
          urlList.add(url);
       }
    }
    
          List<Map<String, String>> list = new ArrayList<>();
          for (Map.Entry<RequestMappingInfo, HandlerMethod> m : map.entrySet()) {
              Map<String, String> map1 = new HashMap<>();
              RequestMappingInfo info = m.getKey();
              HandlerMethod method = m.getValue();
              PatternsRequestCondition p = info.getPatternsCondition();
              for (String url : p.getPatterns()) {
                  map1.put("url", url);
              }
              map1.put("className", method.getMethod().getDeclaringClass().getName()); // 类名
              map1.put("method", method.getMethod().getName()); // 方法名
              RequestMethodsRequestCondition methodsCondition = info.getMethodsCondition();
              for (RequestMethod requestMethod : methodsCondition.getMethods()) {
                  map1.put("type", requestMethod.toString());
              }
              list.add(map1);
          }
    
    
          return list;
      }
    
      private String  getMapper() {
          String result = "";
          RequestMappingHandlerMapping rmhp = SpringContextHolder.getBean(RequestMappingHandlerMapping.class);
          Map<RequestMappingInfo, HandlerMethod> map = rmhp.getHandlerMethods();
          for (RequestMappingInfo info : map.keySet()){
              result += info.getPatternsCondition().toString();
              HandlerMethod hm = map.get(info);
              result += hm.getBeanType().getName();
              result += getMethodParams(hm.getBeanType().getName(),hm.getMethod().getName());
              result += info.getProducesCondition();
          }
          return result;
      }
    
      private String getMethodParams(String className,String methodName){
          String result="";
          try{
              ClassPool pool=ClassPool.getDefault();
              ClassClassPath classPath = new ClassClassPath(this.getClass());
              pool.insertClassPath(classPath);
              CtMethod cm =pool.getMethod(className, methodName);
              // 使用javaassist的反射方法获取方法的参数名
              MethodInfo methodInfo = cm.getMethodInfo();
              CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
              LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);
              result=cm.getName() + "(";
              if (attr == null) {
                  return result + ")";
              }
              CtClass[] pTypes=cm.getParameterTypes();
              String[] paramNames = new String[pTypes.length];
              int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1;
              for (int i = 0; i < paramNames.length; i++) {
                  if(!pTypes[i].getSimpleName().startsWith("HttpServletRe")){
                      result += pTypes[i].getSimpleName();
                      paramNames[i] = attr.variableName(i + pos);
                      result += " " + paramNames[i]+",";
                  }
              }
              if(result.endsWith(",")){
                  result=result.substring(0, result.length()-1);
              }
              result+=")";
          }catch(Exception e){
              e.printStackTrace();
          }
          return result;
      }
  • 相关阅读:
    Android关机流程源码分析
    开关机画面
    android camera
    深入分析AIDL原理
    VMware 虚拟机里连不上网的三种解决方案
    查看Linux中自带的jdk ,设置JAVA_HOME
    Hadoop三种模式安装教程(详解)
    Java ArrayList遍历删除问题
    idea git的使用(四)git建立分支与合并分支
    SpringBoot非官方教程 | 终章:文章汇总
  • 原文地址:https://www.cnblogs.com/exmyth/p/10775477.html
Copyright © 2011-2022 走看看