获取所有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

最新文章

  1. 再次思考 classpath 环境变量 等
  2. 统一SDK接入(U8SDK)——总体思路和架构
  3. HTML5移动开发学习笔记
  4. iOS 获取当前月份的天数(转)、
  5. UBUNTU 下设置全局 path变量
  6. .net通用CMS快速开发框架——问题1:Dapper通用的多表联合分页查询怎么破?
  7. 关于Android自定义view 你所需要知道的基本函数
  8. Chapter 4 Invitations——10
  9. ASP.NET MVC5+EF6+EasyUI 仓库管理系统
  10. NOIP2016 D2-T3 愤怒的小鸟
  11. 如何在页面中获取到ModelAndView绑定的值
  12. Ubuntu14.04设置开机启动脚本(转)
  13. IE haslayout的属性及其值
  14. vim 使用和配置
  15. 计时器---JS
  16. react写单选按钮或table标签
  17. solr学习一(一大堆的学习资料)
  18. tensorflow笔记之滑动平均模型
  19. php 使用html5 XHR2 上传文件 进度显示
  20. malloc free, new delete 的异同点

热门文章

  1. kubernetes跑jenkins动态slave
  2. leetcode10 正则表达式匹配 dp
  3. Leetcode(20)-有效的括号
  4. 一个汉字在php中占几个字节?
  5. webpack OSS All In One
  6. 如何使用 js 检测控制台被用户打开了
  7. copy-webpack-plugin &amp; ignore folder
  8. regex read once bug
  9. pytorch resnet实现
  10. java中synchronized与Lock的异同