首页 > 其他分享 >自动生成接口文档操作手册

自动生成接口文档操作手册

时间:2023-11-25 11:11:17浏览次数:32  
标签:set old 操作手册 %% 接口 openapi 文档 file new

API 文档自动化生成

版本说明

实测仅适用于 Spring 2.x 版本,Spring 3 需要额外配置

后端

创建一个 Spring Web 项目

项目初始化
  • src
    • main
      • java
        • com.example
          • config
            • Knife4jConfig.java
          • entity
            • Entity.java
          • controller
            • EntityController.java
          • Application.java
      • resource
        • application.yaml
项目依赖
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
</dependency>

<dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  <optional>true</optional>
</dependency>
Entity
@Data
@AllArgsConstructor
public class Entity {
    private int id;
    private String name;
}
EntityController
@RestController
@RequestMapping("/entity")
public class EntityController {
    @GetMapping("/get")
    public Entity getEntity() {
        Entity entity = new Entity(1, "Ba11ooner");
        return entity;
    }
}
API 文档生成插件
插件依赖
<dependency>
  <groupId>com.github.xiaoymin</groupId>
  <artifactId>knife4j-spring-boot-starter</artifactId>
  <version>3.0.3</version>
</dependency>
Knife4jConfig
@Configuration
@EnableSwagger2
@Profile("dev")
public class Knife4jConfig {
    @Bean
    public Docket defaultApi2() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        //TODO 配置项目信息
                        .title("project-backend")
                        .description("project-backend")
                        .version("1.0")
                        .build())
                .select()
                //TODO 指定 Controller 扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.example.controller"))
                .paths(PathSelectors.any())
                .build();
    }
}
Application
@SpringBootApplication
@Import(Knife4jConfig.class)
public class Application {

	public static void main(String[] args) {
		SpringApplication.run(ApiDocGeneratorBackendApplication.class, args);
	}

}
application.yaml
spring:
  #swagger 兼容性配置
  mvc:
    pathmatch:
      matching-strategy: ANT_PATH_MATCHER

前端

参考文档

项目初始化

创建一个 Vue 项目(仅以 Vue 为例,其他同理)

API 文档生成插件
依赖下载
cnpm i @umijs/openapi ts-node -D
cnpm i umi-request
生成脚本编写

根目录创建 openapi.config.ts

declare let require: any
const { generateService } = require('@umijs/openapi')

generateService({
    requestLibPath: "import { request } from 'umi'",
    schemaPath:'http://localhost:8080/v3/api-docs',
    serversPath: './src/servers',
})
生成脚本的配置与执行

配置 package 脚本

"scripts": {
  "openapi": "ts-node openapi.config.ts"
},
npm openapi
修改脚本编写

批量修改生成的文本,在根目录下创建文件 openapi.replace.bat or openapi.replace.sh

  • Windows:bat 实现批处理

    @echo off
    chcp 65001 > nul
    
    setlocal enabledelayedexpansion
    
    set "old_text=import { request } from 'umi';"
    set "new_text=import request from 'umi-request';"
    
    set "file_pattern=src/servers/api/*.ts"
    
    for %%f in (%file_pattern%) do (
        set "old_file=%%~nf%%~xf"
        set "new_file=%%~nf_output%%~xf"
    
        set "input_file=src/servers/api/%%~nf%%~xf"
        set "output_file=src/servers/api/%%~nf_output%%~xf"
    
        (for /f "usebackq delims=" %%a in (!input_file!) do (
            set "line=%%a"
            set "line=!line:%old_text%=%new_text%!"
            echo(!line!
        )) > !output_file!
    
        (for /f "usebackq delims=" %%a in (!output_file!) do (
            set "line=%%a"
            set "line=!line!"
            echo(!line!
        )) > !input_file!
    
        del "%cd%\src\servers\api\%%~nf_output%%~xf"
    )
    
  • Unix:shell 实现批处理

    #!/bin/bash
    
    old_text="import { request } from 'umi';"
    new_text="import request from 'umi-request';"
    file_pattern="src/servers/api/*.ts"
    
    for file in $file_pattern; do
        old_file=$(basename "$file")
        new_file="${old_file%.*}_output.${old_file##*.}"
    
        input_file="src/servers/api/$old_file"
        output_file="src/servers/api/$new_file"
    
        sed -e "s/$old_text/$new_text/g" "$input_file" > "$output_file"
        mv "$output_file" "$input_file"
    done
    
    sh openapi.replace.sh
    
修改脚本的配置和执行
{
  "scripts": {
    "openapi-create": "ts-node openapi.config.ts",
    "openapi-replace": "openapi.replace.bat",
    "openapi": "npm run openapi-create && npm run openapi-replace"
  }
}
npm run openapi

标签:set,old,操作手册,%%,接口,openapi,文档,file,new
From: https://www.cnblogs.com/ba11ooner/p/17855316.html

相关文章

  • 西安体院篮球专项队训练现状及策略研究——论文文档
    摘 要:随着时代的进步,篮球已成为一项全民健身的活动。为了满足人们日益增长的需求,培养优秀的篮球运动员已成为当务之急。为此,提升运动员的训练水平和积极性显得尤为重要。只有通过不断努力,才能真正让他们的技术和实战水平达到最佳状态。通过问卷调查和因素分析法,我们对西安体育学......
  • 施工环境对施工质量影响的研究——论文文档
    研究意义工程质量监督管理是工程项目全过程中质量管理工作的重要内容,产品质量重要程度直接决定了建筑施工质量管理水平。但近年来,企业虽对项目中工程质量监督管理的关注程度日益增强,但针对工程施工涉及面较广、过程复杂繁杂的特点,在建筑工程质量管理体系方面仍需要完善。影响......
  • 实现 利用SPI 接口 发送 显示屏 初始化代码
    1`timescale1ns/1ns2modulelcd_spi_top_tb();34regclk_x2_i;5regrst_i;6wirespi_cs;7wirespi_scl;8wirespi_mosi;9wirespi_miso;10wirespi_init_done;111213initialbegin14clk_x2_i=0;15rst_i=0;16......
  • 16位 565 DVP 接口实现
    1modulelcd_dvp_drive2#(3parameterVBP=2,//OK4parameterVFP=36,//OK5parameterVSW=6,//OK6parameterVDP=480,//52478parameterHBP=8,//ok9parameterHFP=96,//ok10parameterHSW=16,//ok11parameter......
  • Java Web 实现文件上传和下载接口功能
    1.上传java代码实现@ResponseBody@PostMapping("/upload")publicResponseVoupload(@RequestParam(value="file",required=false)MultipartFilemultipartFile){Filefile=newFile("上传到服务器的文件地址");try{......
  • 『接口测试干货』| Newman+Postman接口自动化测试完整过程
    (『接口测试干货』|Newman+Postman接口自动化测试完整过程)1Newman简介Newman是Postman的一个扩展库(NodeJs库);Newman+Postman可完成接口自动化测试工作;Postman导出的JSON格式文件可通过Newman的命令行执行;因为Postman运行后只有概要结果,没有像其他自动化测试框架那么完美漂......
  • 基于django的4s店客户管理系统-计算机毕业设计源码+LW文档
    摘 要 进入21世纪网络和计算机得到了飞速发展,并和生活进行了紧密的结合。目前,网络的运行速度以达到了千兆,覆盖范围更是深入到生活中的角角落落。这就促使管理系统的发展。网上办公可以实现远程处理事务,远程提交工作和随时追踪工作的状态。网上管理系统给人们带来前所未有的体......
  • map 接口的遍历
    packagecom.wxledu.map_;importjava.util.*;@SuppressWarnings({"all"})publicclassMapFor{publicstaticvoidmain(String[]args){Mapmap=newHashMap();map.put("邓超","孙俪");map.put("......
  • 【Python进阶笔记】md文档笔记第6篇:Python进程和多线程使用(图文和代码)
    本文从14大模块展示了python高级用的应用。分别有Linux命令,多任务编程、网络编程、Http协议和静态Web编程、html+css、JavaScript、jQuery、MySql数据库的各种用法、python的闭包和装饰器、mini-web框架、正则表达式等相关文章的详细讲述。全套md格式笔记和代码自取:请移步这里......
  • 5分钟掌握接口自动化测试,4个知识点简单易学!
    一.什么是接口测试接口测试是一种软件测试方法,用于验证不同软件组件之间的通信接口是否按预期工作。在接口测试中,测试人员会发送请求并检查接收到的响应,以确保接口在不同场景下都能正常工作。就工具而言,常见的测试工具有Jmeter、Postman等。但这类工具往往更是做接口调试,对于做......