springboot swagger2 gradle生成离线文档

版权声明:本文为博主原创文章,未经允许不得转发 https://blog.csdn.net/fengchen0123456789/article/details/83508512

这种方式和maven生成文档不一样,分2步[应该可以一步生成,奈何插件不会写],打开命令窗口,切换到项目所在目录,执行如下命令

./gradlew generateSwaggerDocumentation

上述命令命令执行成功之后,继续执行如下命令

./gradlew test --tests *SpringbootSwaggerApplicationTests

gradle配置

buildscript {
    ext {
        springBootVersion = '2.0.6.RELEASE'
    }
    repositories {
        mavenLocal()
        mavenCentral()
        jcenter()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath 'com.benjaminsproule:swagger-gradle-plugin:+'
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'com.benjaminsproule.swagger'

group = 'com.lf.swagger'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

swagger {
    apiSource {
        springmvc = true
        // 扫描的包路径
        locations = ['com.lf.swagger.springbootswagger']
        schemes = ['http', 'https']
        host = 'www.example.com:8080'
        basePath = '/api'
        info {
            title = 'Swagger Gradle Plugin Sample'
            version = 'v1'
            description = 'This is a sample.'
            termsOfService = 'http://www.example.com/termsOfService'
            contact {
                email = '[email protected]'
                name = 'Name'
                url = 'http://www.example.com'
            }
            license {
                url = 'http://www.apache.org/licenses/LICENSE-2.0.html'
                name = 'Apache 2.0'
            }
        }
        outputPath = "${project.rootDir}/generated/document.html"
        swaggerDirectory = "${project.rootDir}/generated/swagger-ui"
        attachSwaggerArtifact = true
    }
}

dependencies {
    compile "io.swagger:swagger-annotations:1.5.21"

    compile "org.springframework.boot:spring-boot-starter-web:$springBootVersion"
    compile('org.springframework.boot:spring-boot-starter-test:2.0.6.RELEASE')

    // swagger
    compile('io.springfox:springfox-swagger2:2.6.1')
    compile('io.springfox:springfox-swagger-ui:2.6.1')
    compile('io.springfox:springfox-staticdocs:2.6.1')
    testCompile('org.springframework.restdocs:spring-restdocs-mockmvc:2.0.2.RELEASE')
}

controller

@RestController
@Api("HelloController 相关接口说明")
@ApiResponses({
        @ApiResponse(code = 200, message = "OK"),
        @ApiResponse(code = 400, message = "客户端请求错误"),
        @ApiResponse(code = 404, message = "找不到路径"),
        @ApiResponse(code = 500, message = "编译异常")
})
public class HelloController {

    @ApiOperation(value = "查询某个设备信息", notes = "查询某个设备信息 1")
    @GetMapping("/getDevice/{id}")
    public String getDevice(@ApiParam(name = "id", value = "设备的唯一编号", required = true) @PathVariable int id) {
        return "hello swagger2: "+ id;
    }

}

SpringbootSwaggerApplicationTests

这个类实是在测试文件中的

import io.github.robwin.markup.builder.MarkupLanguage;
import io.github.robwin.swagger2markup.GroupBy;
import io.github.robwin.swagger2markup.Swagger2MarkupConverter;
import org.junit.Test;
//@RunWith(SpringRunner.class)
//@SpringBootTest
public class SpringbootSwaggerApplicationTests {

    private String snippetDir = "/generated/snippets";
    private String outputDir = "generated/swagger-ui";

    @Test
    public void doIt() throws Exception{
        // 读取上一步生成的swagger.json转成asciiDoc,写入到outputDir
        Swagger2MarkupConverter.from(outputDir + "/swagger.json")
                .withPathsGroupedBy(GroupBy.TAGS)// 按tag排序
                .withMarkupLanguage(MarkupLanguage.MARKDOWN)// 格式
                .withExamples(snippetDir)
                .build()
                .intoFolder(outputDir);// 输出
    }

}

效果图

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/fengchen0123456789/article/details/83508512