• 获取SpringBoot中所有的url和其参数


    获取所有url和方法的对应关系

     1 @Data
     2 public class Param {
     3 
     4     /**
     5      * 字段名称
     6      */
     7     private String name;
     8 
     9     /**
    10      *
    11      */
    12     private String in;
    13 
    14     /**
    15      * 字段说明
    16      */
    17     private String description;
    18 
    19     /**
    20      * 字段是否必填
    21      */
    22     private String required;
    23 
    24     /**
    25      * 字段类型
    26      */
    27     private String type;
    28 
    29 }
    Param
     1 @Data
     2 public class VelocityTemplate {
     3 
     4     /**
     5      * api 标题
     6      */
     7     private String title;
     8 
     9     /**
    10      * url 名称
    11      */
    12     private String urlName;
    13 
    14     /**
    15      * 请求方法
    16      */
    17     private String requestMethod;
    18 
    19     /**
    20      * 方法描述
    21      */
    22     private String description;
    23 
    24     /**
    25      * 参数列表
    26      */
    27     private List<Param> params;
    28 
    29     /**
    30      * 参数数量
    31      */
    32     private int paramsNum;
    33 
    34     /**
    35      * 请求体格式
    36      */
    37     private String request;
    38 
    39     /**
    40      * 返回体格式
    41      */
    42     private String response;
    43 
    44 
    45 }
    VelocityTemplate
    
    
    @Autowired
    private RequestMappingHandlerMapping mappingHandlerMapping;
    
     
     1 public List<?> getAllUrl() throws IOException {
     2         List<VelocityTemplate> urlList = new ArrayList<>();
     3         Map<RequestMappingInfo, HandlerMethod> map = mappingHandlerMapping.getHandlerMethods();
     4         for (Map.Entry<RequestMappingInfo, HandlerMethod> methodEntry : map.entrySet()) {
     5             VelocityTemplate velocityTemplate = new VelocityTemplate();
     6             RequestMappingInfo info = methodEntry.getKey();
     7             HandlerMethod method = methodEntry.getValue();
     8             PatternsRequestCondition patternsRequestCondition = info.getPatternsCondition();
     9             for (String url : patternsRequestCondition.getPatterns()) {
    10                 velocityTemplate.setUrlName(url);
    11             }
    12 
    13             RequestMethodsRequestCondition methodsRequestCondition = info.getMethodsCondition();
    14             String type = methodsRequestCondition.toString();
    15             if (type.startsWith("[") && type.endsWith("]")) {
    16                 type = type.substring(1, type.length() - 1);
    17                 // 方法名
    18                 velocityTemplate.setRequestMethod(type);
    19             }
    20             if (StringUtils.hasText(type)) {
    21                 velocityTemplate.setTitle("###" + (urlList.size() + 1) + "、" + (method.hasMethodAnnotation(ApiOperation.class) ?
    22                         method.getMethodAnnotation(ApiOperation.class).value() : ""));
    23                 velocityTemplate.setDescription(method.toString());
    24                 MethodParameter[] methodParameters = method.getMethodParameters();
    25                 List<Param> params = new ArrayList<>();
    26                 for (MethodParameter methodParameter : methodParameters) {
    27                     if (ObjectUtils.isEmpty(methodParameter.getParameterAnnotations()) || methodParameter.hasParameterAnnotation(Valid.class)) {
    28                         ReflectionUtils.FieldCallback fieldCallback = field -> {
    29                             ApiModelProperty annotation = field.getAnnotation(ApiModelProperty.class);
    30                             Param param = new Param();
    31                             if (annotation != null) {
    32                                 param.setName(annotation.name());
    33                                 param.setDescription(annotation.value());
    34                             } else {
    35                                 param.setName(field.getName());
    36                                 param.setDescription("");
    37                             }
    38                             param.setType(field.getType().getTypeName());
    39                             param.setRequired("是");
    40                             params.add(param);
    41                         };
    42                         if (methodParameter.getParameterType().getName().equals(HttpServletRequest.class.getName()) || methodParameter.getParameterType().getName().equals(HttpServletResponse.class.getName())) {
    43                             continue;
    44                         }
    45                         ReflectionUtils.doWithFields(methodParameter.getParameterType(), fieldCallback,
    46                                 field -> !field.getName().equalsIgnoreCase("serialVersionUID"));
    47                     } else if (methodParameter.hasParameterAnnotation(RequestParam.class)) {
    48                         RequestParam requestParamAnnotation =
    49                                 methodParameter.getParameterAnnotation(RequestParam.class);
    50                         ApiParam apiParam = methodParameter.getParameterAnnotation(ApiParam.class);
    51                         Param param = new Param();
    52                         param.setName(requestParamAnnotation.name());
    53                         param.setRequired(requestParamAnnotation.required() ? "是" : "否");
    54                         param.setType(methodParameter.getParameterType().getTypeName());
    55                         param.setDescription(apiParam == null ? "" : apiParam.value());
    56                         params.add(param);
    57                     }
    58                 }
    59                 velocityTemplate.setParams(params);
    60                 urlList.add(velocityTemplate);
    61             }
    62         }
    63         GenerateWikiApiDocument.generateFile(urlList, "Statistics");
    64         return urlList;
    65     }
    getAllUrl
     1 public static void generateFile(List<VelocityTemplate> velocityTemplates, String controllerClass) throws IOException {
     2         String basePath = "C:\api\";
     3         FileWriter file = new FileWriter(basePath + controllerClass +"API.md", false);
     4         BufferedWriter bufferedWriter = new BufferedWriter(file);
     5         //设置velocity资源加载器
     6         Properties prop = new Properties();
     7         prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader" );
     8         Velocity.init(prop);
     9 
    10         Map<String, List<VelocityTemplate>> listMap = new HashMap<>();
    11         listMap.put("list", velocityTemplates);
    12         VelocityContext context = new VelocityContext(listMap);
    13         String templatePath = "velocity/swagger.md.vm";
    14         Template tpl = Velocity.getTemplate(templatePath, "UTF-8" );
    15         tpl.merge(context, bufferedWriter);
    16         bufferedWriter.close();
    17         file.close();
    18     }
    GenerateWikiApiDocument#generateFile
     1 #foreach($item in $list)
     2 ${item.title}
     3 
     4 
     5 <table>
     6     <tr>
     7         <td>URL</td>
     8         <td colspan="4">${item.urlName}</td>
     9     </tr>
    10     <tr>
    11         <td>请求方式</td>
    12         <td colspan="4">${item.requestMethod}</td>
    13     </tr>
    14     <tr>
    15         <td>方法名</td>
    16         <td colspan="4">${item.description}</td>
    17     </tr>
    18     <tr>
    19         #set($size=${item.params.size()} + 3)
    20         <td rowspan="$size">参数格式</td>
    21         <td>字段</td>
    22         <td>字段类型</td>
    23         <td>是否必填</td>
    24         <td>说明</td>
    25     </tr>
    26     #foreach($param in $item.params)
    27     <tr>
    28         <td>${param.name}</td>
    29         <td>${param.type}</td>
    30         <td>${param.required}</td>
    31         <td>${param.description}</td>
    32     </tr>
    33     #end
    34      <tr>
    35         <td>请求参数</td>
    36         <td colspan="3">${item.request}</td>
    37     </tr>
    38     <tr>
    39         <td>返回参数</td>
    40         <td colspan="3">${item.response}</td>
    41     </tr>
    42 </table>
    43 
    44 #end
    velocitytemplate
  • 相关阅读:
    天大复试机试练习_003
    C++随手记--字符串转数字
    C++标准库STL 之 我觉得应该有的方法——split
    apt-get 详解&&配置阿里源
    Nginx 图文详解
    MySQL数据库管理常用命令小结
    oracle数据库备份
    SqlServer数据库备份还原步骤
    mysql数据备份与恢复
    Tomcat架构
  • 原文地址:https://www.cnblogs.com/ice-image/p/14330964.html
Copyright © 2020-2023  润新知