springboot swagger2 gradle生成離線文件
阿新 • • 發佈:2018-12-18
這種方式和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);// 輸出
}
}