首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >Spring Boot 项目Swagger 注解@Schema转换为word文档

Spring Boot 项目Swagger 注解@Schema转换为word文档

原创
作者头像
JQ实验室
发布2025-07-31 14:35:22
发布2025-07-31 14:35:22
1410
举报
文章被收录于专栏:java基础教程java基础教程
代码语言:java
复制
 public static List<FieldInfo> parseModel(Class<?> clazz) {
        List<FieldInfo> fieldInfos = new ArrayList<>();
        Field[] fields = clazz.getDeclaredFields();

        for (Field field : fields) {
            io.swagger.v3.oas.annotations.media.Schema schema = field.getAnnotation(io.swagger.v3.oas.annotations.media.Schema.class);
            if (schema != null) {
                FieldInfo fieldInfo = new FieldInfo();
                fieldInfo.setName(field.getName());
                fieldInfo.setType(field.getType().getSimpleName());
                fieldInfo.setDescription(schema.description());
                fieldInfos.add(fieldInfo);
            }
        }

        return fieldInfos;
    }

    public static void parseOpenAPI(String filePath) throws Exception {
        // 解析 OpenAPI 文档
        OpenAPI openAPI = new OpenAPIV3Parser().read(filePath);
        // 获取模型定义
        Map<String, Schema> schemas = openAPI.getComponents().getSchemas();

        Map<String, List<FieldInfo>> fieldMap = new HashMap<>();

        for (Map.Entry<String, Schema> entry : schemas.entrySet()) {
            String modelName = entry.getKey();
            Schema schema = entry.getValue();

            System.out.println("Model: " + modelName);
            //System.out.println("Description: " + schema.getDescription());
            //System.out.println("Properties: " + schema.getProperties());
            Map<String, Schema> properties = schema.getProperties();
            List<FieldInfo> fieldInfos = new ArrayList<>();
            for (Map.Entry<String, Schema> propertyEntry : properties.entrySet()) {
                String propertyName = propertyEntry.getKey();
                Schema propertySchema = propertyEntry.getValue();

//                System.out.println("  Property: " + propertyName);
//                System.out.println("    Type: " + propertySchema.getType());
//                System.out.println("    Description: " + propertySchema.getDescription());
                FieldInfo fieldInfo = new FieldInfo(propertyName, propertySchema.getType(), propertySchema.getDescription());
                fieldInfos.add(fieldInfo);
            }
            fieldMap.put(modelName, fieldInfos);
        }

        generateWord(fieldMap, "D:\\swagger.docx");

    }

    /**
     * 添加标题到文档
     */
    private static void addTitle(XWPFDocument document, String titleText, int level) {
        XWPFParagraph title = document.createParagraph();
        title.setAlignment(ParagraphAlignment.CENTER); // 居中对齐
        title.setStyle("Heading" + level); // 设置标题样式

        XWPFRun titleRun = title.createRun();
        titleRun.setText(titleText);
        titleRun.setBold(true);
        titleRun.setFontSize(18);
    }

    /**
     * 添加段落到文档
     */
    private static void addParagraph(XWPFDocument document, String paragraphText) {
        XWPFParagraph paragraph = document.createParagraph();
        paragraph.setAlignment(ParagraphAlignment.LEFT); // 左对齐

        XWPFRun run = paragraph.createRun();
        run.setText(paragraphText);
        run.setFontSize(12);
    }

    private static void addTable(XWPFDocument document, List<FieldInfo> fieldInfos){
        // 创建表格
        XWPFTable table = document.createTable();
        table.setWidth("100%");

        // 添加表头
        XWPFTableRow headerRow = table.getRow(0);
        headerRow.getCell(0).setText("字段");
        headerRow.addNewTableCell().setText("类型");
        headerRow.addNewTableCell().setText("说明");

        // 添加数据行
        for (FieldInfo fieldInfo : fieldInfos) {
            XWPFTableRow dataRow = table.createRow();
            dataRow.getCell(0).setText(fieldInfo.getName());
            dataRow.getCell(1).setText(fieldInfo.getType());
            dataRow.getCell(2).setText(fieldInfo.getDescription());
        }

    }

    public static void generateWord(Map<String, List<FieldInfo>> fieldMap, String filePath) throws Exception {
        // 创建 Word 文档
        XWPFDocument document = new XWPFDocument();
        fieldMap.forEach((modelName, fieldInfos) -> {
            addParagraph(document, modelName);
            addParagraph(document, "");
            addTable(document, fieldInfos);
            addParagraph(document, "");
        });
        // 保存文档
        try (FileOutputStream out = new FileOutputStream(filePath)) {
            document.write(out);
        }

        document.close();
    }

    
    private static class FieldInfo {
        String name;
        String type;
        String description ;

        public FieldInfo() {
        }

        public FieldInfo(String name, String type, String description) {
            this.name = name;
            this.type = type;
            this.description = description;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }
    }

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档