天天看点

Api 接口统计工具类前言实践

前言

我们遇到公司让统计接口,一个项目中接口很多不能真的一个一个统计吧,为了提高统计效率。写一个统计工具类。

实践

一、添加maven依赖

<dependency>
            <groupId>org.reflections</groupId>
            <artifactId>reflections</artifactId>
            <version>0.9.11</version>
            <scope>compile</scope>
        </dependency>
   <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.17</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.17</version>
        </dependency>
           

二、代码实践

@Slf4j
public class ApiGenerateUtil {

    private ApiGenerateUtil() {
    }

    public static void main(String[] args) throws Exception {

        //创建工作薄
        FileOutputStream output = new FileOutputStream(new File("C:\\Users\Desktop\\newFile\\new11.xlsx"));
        // 声明一个工作薄
        SXSSFWorkbook workbook = new SXSSFWorkbook();
        CellStyle style = workbook.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        style.setBorderTop(BorderStyle.THIN);
        SXSSFSheet sheet = workbook.createSheet("sheet");
        Reflections reflections = new Reflections("com.demo.controller");
        Set<Class<?>> enumClasses = reflections.getTypesAnnotatedWith(RestController.class);


        int i = 0;
        for (Class<?> m : enumClasses) {
            try {
                String baseUrlValue = "";
                String tag = "";

                RequestMapping requestMapping = m.getAnnotation(RequestMapping.class);
                if(requestMapping != null){
                    baseUrlValue = requestMapping.value()[0];
                }

                Api annotation = m.getAnnotation(Api.class);
                if (annotation != null) {
                    tag = annotation.tags()[0];
                }

                Method[] methods = m.getDeclaredMethods();
                for (Method method : methods) {
                    PutMapping putMapping = method.getAnnotation(PutMapping.class);
                    GetMapping getMapping = method.getAnnotation(GetMapping.class);
                    PostMapping postMapping = method.getAnnotation(PostMapping.class);
                    DeleteMapping deleteMapping = method.getAnnotation(DeleteMapping.class);
                    ApiOperation apiOperation = method.getAnnotation(ApiOperation.class);

                    String urlValue = "";
                    String nameValue = "";
                    String requestType = "";

                    if(getMapping != null){
                        requestType = "get";
                        if(getMapping.value().length > 0){
                            urlValue = getMapping.value()[0];
                        }
                    }else if(postMapping != null){
                        requestType = "post";
                        if(postMapping.value().length > 0){
                            urlValue = postMapping.value()[0];
                        }
                    }else if(putMapping != null){
                        requestType = "put";
                        if(putMapping.value().length > 0){
                            urlValue = putMapping.value()[0];
                        }
                    }else if(deleteMapping != null){
                        requestType = "delete";
                        if(deleteMapping.value().length > 0){
                            urlValue = deleteMapping.value()[0];
                        }
                    }

                    if(StrUtil.isBlank(requestType)){
                        continue;
                    }

                    if(apiOperation != null){
                        nameValue = apiOperation.value();
                    }


                    SXSSFRow row = sheet.createRow(i++);
                    SXSSFCell cell0 = row.createCell(0);
                    cell0.setCellStyle(style);
                    cell0.setCellValue(tag);

                    SXSSFCell cell1 = row.createCell(1);
                    cell1.setCellStyle(style);
                    cell1.setCellValue(nameValue);

                    SXSSFCell cell2 = row.createCell(2);
                    cell2.setCellStyle(style);
                    cell2.setCellValue(requestType);

                    SXSSFCell cell3 = row.createCell(3);
                    cell3.setCellStyle(style);
                    cell3.setCellValue( baseUrlValue + urlValue );
                }
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        }


        //把创建的内容写入到输出流中,并关闭输出流
        // 提示下载文件
        // httpServletResponse.reset();
        //设置响应头,
        workbook.write(output);
        output.flush();
        output.close();
    }
}