一个方法获取所有controller的接口及swagger注释并导出excel


    @Autowired
    private WebApplicationContext applicationContext;
    @ApiOperation(value = "找接口路径", notes = "找接口路径")
    @PostMapping("/api/common/getInterfacePath")
    public void getInterfacePath(HttpServletResponse response){
        RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
        // 获取url与类和方法的对应信息
        Map<RequestMappingInfo, HandlerMethod> map = mapping.getHandlerMethods();

        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        for (Map.Entry<RequestMappingInfo, HandlerMethod> m : map.entrySet()) {
            Map<String, String> map1 = new HashMap<String, String>();
            RequestMappingInfo info = m.getKey();
            HandlerMethod method = m.getValue();
            //获取当前方法所在类名
//            Class<?> bean = method.getBeanType();
            //获取方法上注解以及注解值
            ApiOperation methodAnnotation = method.getMethodAnnotation(ApiOperation.class);

            PatternsRequestCondition p = info.getPatternsCondition();
            for (String url : p.getPatterns()) {
                if (url.length()>11){
                    String substring = url.substring(0, 11);
                    if (substring.equals("/api/common")){
                        //接口名
                        map1.put("url", url);
                        //methodAnnotation.value() swagger注释value
                        map1.put("value",ObjectUtils.isNotEmpty(methodAnnotation)? methodAnnotation.value() : null);
                        list.add(map1);
                    }
                }
/*                map1.put("className", method.getMethod().getDeclaringClass().getName()); // 类名
                map1.put("method", method.getMethod().getName()); // 方法名*/

            }
        }
        try {
            ExcelData data = new ExcelData();
            List<List<Object>> rows = new ArrayList();
            data.setName("一县一品-乡村振兴裸奔接口导出");//sheet表名称
            //表头
            List<String> titles = new ArrayList();
            titles.add("接口地址");
            titles.add("接口名称");
            data.setTitles(titles);
            for (Map<String,String> map1 : list) {
                List<Object> row1 = new ArrayList();
                row1.add(map1.get("url"));
                row1.add(map1.get("value"));
                rows.add(row1);
            }
            data.setRows(rows);
            ExportExcelUtils.exportExcel(response,  "乡村振兴裸奔接口导出.xlsx" + new Date(), data);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值