springboot swagger生成文档

Swagger 生成html api文档


测试过程发现使用插件生成swagger.json一直失败,下载官方示例却是正常,各种配置都检查过没有发现问题所在,目前没有解决,仅作为学习,暂时没有去深究其原因。


通过方法生成

  • 生成过程
接口生成原理:
    1)使用Springfox-swagger2生成swagger.json文件
    2)使用Swagger2markup将swagger.json文件转换成asciidoc文档片段,包括(definitions.adoc、overview.adoc、paths.adoc、security.adoc)
    3)编写asciidoc的文档(主要是组装步骤2中生成的asciidoc文档片段)
    4)使用Asciidoctor将asciidoc转换成HTML或pdf
  • 引入pom依赖
        <!--swagger-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>${swagger.version}</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${swagger.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>io.swagger</groupId>
                    <artifactId>swagger-annotations</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>io.swagger</groupId>
                    <artifactId>swagger-models</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>1.5.21</version>
        </dependency>
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-models</artifactId>
            <version>1.5.21</version>
        </dependency>

        <dependency>
            <groupId>io.github.swagger2markup</groupId>
            <artifactId>swagger2markup-maven-plugin</artifactId>
            <version>1.3.3</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.asciidoctor</groupId>
            <artifactId>asciidoctorj</artifactId>
            <version>1.5.4.1</version>
            <scope>test</scope>
        </dependency>
  • 执行GenerateHtml测试方法

通过插件生成

很遗憾,发现springboot项目无论如何也无法生成正常的swagger.json,但是我们可以使用代码来生成

  • 生成过程
1)生成swagger.json
2) 生成snippets
3)将asciidoc生成html、pdf
  • 相关配置
参考pom.xml

通过插件生成

git示例中的项目使用的是JAX-RS,经测试也是正常

  • 生成过程
1)生成swagger.json
2) 自定义adoc模板
3)将生成的snippets与模板结合
4)生成html文档
  • 引入插件
swagger-maven-plugin
  • 参考

官网示例
源码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot Swagger(现在更常用的是Springfox)是一个用于文档生成的强大工具,它允许你在Spring应用程序中轻松地添加API文档。配置Servers主要是为了指定API的上下文路径以及基础URL,以便生成文档能够准确反映应用的实际部署位置。 在Swagger的配置中,你可以通过`@EnableSwagger2`注解启用Swagger,并在`application.yml`或`application.properties`文件中设置相关的服务器信息。以下是基本的配置步骤: 1. 添加依赖:在你的pom.xml或build.gradle文件中加入Springfox的依赖。 ```xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>3.x.x</version> <!-- 使用最新的版本 --> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>3.x.x</version> </dependency> ``` 2. 配置application.yml(YAML格式): ```yaml swagger: servers: - url: http://localhost:8080/api-docs # 本地开发环境 description: "本地开发服务器" - url: https://your-api.example.com/docs # 生产环境 description: "生产环境API地址" ``` 或者application.properties (properties格式): ```properties swagger.servers.url=http://localhost:8080/api-docs swagger.servers.url=https://your-api.example.com/docs swagger.servers.description=生产环境API地址 ``` 在这里,`url`字段指定了API的基础URL,`description`则是描述性的名称,有助于用户理解每个服务器的作用。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值