最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Gradle環(huán)境下導(dǎo)出Swagger為PDF的步驟詳解

 更新時間:2019年06月26日 08:25:39   作者:鵬徙南暝  
這篇文章主要介紹了Gradle環(huán)境下導(dǎo)出Swagger為PDF的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧

說明

我個人是一直使用Swagger作為接口文檔的說明的。但是由于在一些情況下,接口文檔說明需要以文件的形式交付出去,如果再重新寫一份文檔難免有些麻煩。于是在網(wǎng)上看到了Swagger2Markup + asciidoctor導(dǎo)出PDF的方法,百度一番后感覺網(wǎng)上的文章還是有很多沒有描述清楚的地方,遂還是硬著頭皮把官方的英文文檔大致瀏覽了一下,按照自己的思路整理出具體的步驟。

本文用到的工具:

  • Gradle - 4.10.3
  • SpringBoot - 2.1.6.RELEASE
  • Swagger - 2.9.2
  • Swagger2Markup - 1.3.3
  • asciidoctor
  • spring-restdocs-mockmvc

準(zhǔn)備Swagger數(shù)據(jù)

SpringBoot中使用Swagger的過程就不再贅述了,下面是本文使用的范例:

@Configuration
@EnableSwagger2
class SwaggerConfig {
  @Bean
  public Docket createRestApi() {
    return new Docket(DocumentationType.SWAGGER_2)
      .apiInfo(apiInfo())
      .select()
      .apis(RequestHandlerSelectors.basePackage("com.jptangchina.gradle.controller"))
      .paths(PathSelectors.any())
      .build();
  }

  private ApiInfo apiInfo() {
    return new ApiInfoBuilder()
      .title("Swagger2Markup Test Api")
      .version("1.0")
      .build();
  }
}
@RestController
@RequestMapping("/user")
@Api(tags = "用戶接口")
public class UserController {

  @ApiOperation("用戶登錄")
  @ResponseBody
  @PostMapping("/login")
  public Result<Void> login(
    @ApiParam(value = "用戶名", example = "jptangchina", required = true) @RequestParam String username,
    @ApiParam(value = "密碼", example = "jptangchina", required = true) @RequestParam String password) {
    return Result.ok();
  }
}

使用org.asciidoctor.convert生成PDF(個人不推薦使用)

官方教程地址:https://github.com/Swagger2Markup/spring-swagger2markup-demo

僅為了簡單的導(dǎo)出PDF而言,本文針對官方案例均有所改動,去掉了部分沒有用到的配置。

1. 獲取Swagger json文件

Swagger頁面本質(zhì)上也就是對json文件進(jìn)行解析。這里需要先編寫單元測試訪問/v2/api-docs接口并將json文件保存到本地。

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
class SwaggerTest {
  @Autowired
  private MockMvc mockMvc;
  @Test
  public void generateAsciiDocsToFile() throws Exception {
    String outputDir = System.getProperty("io.springfox.staticdocs.outputDir");
    MvcResult mvcResult = this.mockMvc.perform(get("/v2/api-docs")
      .accept(MediaType.APPLICATION_JSON))
      .andExpect(status().isOk())
      .andReturn();

    MockHttpServletResponse response = mvcResult.getResponse();
    String swaggerJson = response.getContentAsString();
    Files.createDirectories(Paths.get(outputDir));
    try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(outputDir, "swagger.json"), StandardCharsets.UTF_8)){
      writer.write(swaggerJson);
    }
  }

}

System.getProperty("io.springfox.staticdocs.outputDir");來自于build.gradle中的配置

2. 將json文件轉(zhuǎn)換為adoc文件

轉(zhuǎn)換json文件需要使用到io.github.swagger2markup插件的convertSwagger2markup方法。

引入相關(guān)依賴:

buildscript {
  ...
  dependencies {
    ...
    classpath 'io.github.swagger2markup:swagger2markup-gradle-plugin:1.3.3'
  }
}
 
apply plugin: 'io.github.swagger2markup'

配置convertSwagger2markup:

ext {
  asciiDocOutputDir = file("${buildDir}/asciidoc")
  swaggerOutputDir = file("${buildDir}/swagger")
}

test {
  systemProperty 'io.springfox.staticdocs.outputDir', swaggerOutputDir
}

convertSwagger2markup {
  dependsOn test
  swaggerInput "${swaggerOutputDir}/swagger.json"
  outputDir asciiDocOutputDir
  config = [
      'swagger2markup.pathsGroupedBy' : 'TAGS',
  ]
}

更多config配置可以參考:http://swagger2markup.github.io/swagger2markup/1.3.3/#_swagger2markup_properties

3. 將adoc文件轉(zhuǎn)換為PDF文件

轉(zhuǎn)換PDF文件需要用到org.asciidoctor.convert插件的asciidoctor方法。

引入相關(guān)依賴:

buildscript {
  ...
  dependencies {
    ...
    classpath 'org.asciidoctor:asciidoctor-gradle-plugin:1.5.3'
  }
}
apply plugin: 'org.asciidoctor.convert'

手動編寫index.adoc文件,放置到${asciiDocOutputDir.absolutePath}中:

include::{generated}/overview.adoc[]
include::{generated}/paths.adoc[]
include::{generated}/definitions.adoc[]
include::{generated}/security.adoc[]

{generated}默認(rèn)值為${build}/asciidoc,參見:https://github.com/Swagger2Markup/swagger2markup-gradle-project-template

配置asciidoctor:

asciidoctor {
  dependsOn convertSwagger2markup
  // sourceDir中需要包含有之前手動編寫的index.adoc文件
  sourceDir(asciiDocOutputDir.absolutePath)
  sources {
    include "index.adoc"
  }
  backends = ['pdf']
  attributes = [
      doctype: 'book',
      toc: 'left',
      toclevels: '3',
      numbered: '',
      sectlinks: '',
      sectanchors: '',
      hardbreaks: '',
      generated: asciiDocOutputDir
  ]
}

4. 編寫一個自定義task用來執(zhí)行上述流程:

task genPdf(type: Test, dependsOn: test) {
  include '**/*SwaggerTest.class'
  exclude '**/*'
  dependsOn(asciidoctor)
}

執(zhí)行g(shù)enPdf,就可以生成Swagger對應(yīng)的PDF文件。

5. 小結(jié)

使用此方法步驟還是比較繁瑣的,總體來講就是json -> adoc -> pdf,并且使用此種方法目前有幾個比較大的問題我仍然沒有找到解決方案:

  • 從官方文檔中可以看到支持的語言默認(rèn)有EN, DE, FR, RU。沒錯,不支持CN,從導(dǎo)出的文檔也可以看到,部分中文無法顯示,目前我也尚未找到是否有配置可以實(shí)現(xiàn)這個功能。網(wǎng)上的文章部分是通過替換源代碼包里面的字體文件來實(shí)現(xiàn),但是由于后面有更好的解決方案,這里就不再討論。
  • 從asciidoctorj-pdf的1.5.0-alpha.16版本以后(目前最新是1.5.0-alpha.18),這種方式生成文件會拋出異常,我個人并沒有深究這個異常,有興趣的讀者可以通過修改版本號試一試。
  • 生成的adoc文件一般包含overview.adoc、paths.adoc、definitions.adoc、security.adoc一共4個文件,這也是為什么要手動編寫index.adoc進(jìn)行整合的原因,感覺不太方便。

綜上,我個人并不推薦采用此方式生成PDF。

build.gradle完整文件參考:

buildscript {
  ext {
    springbootVersion = '2.1.6.RELEASE'
  }
  repositories {
    maven {
      url 'http://maven.aliyun.com/nexus/content/groups/public'
    }
  }
  dependencies {
    classpath "org.springframework.boot:spring-boot-gradle-plugin:${springbootVersion}"
    classpath 'org.asciidoctor:asciidoctor-gradle-plugin:1.5.3'
    classpath 'io.github.swagger2markup:swagger2markup-gradle-plugin:1.3.3'
  }
}

repositories {
  maven {
    url 'http://maven.aliyun.com/nexus/content/groups/public'
  }
}

apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'io.github.swagger2markup'
apply plugin: 'org.asciidoctor.convert'

group 'com.jptangchina'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8
targetCompatibility = 1.8

ext {
  asciiDocOutputDir = file("${buildDir}/asciidoc")
  swaggerOutputDir = file("${buildDir}/swagger")
  swaggerVersion = '2.9.2'
}

dependencies {
  compile 'org.springframework.boot:spring-boot-starter-web'
  compile "io.springfox:springfox-swagger2:${swaggerVersion}"
  compile "io.springfox:springfox-swagger-ui:${swaggerVersion}"
  compile 'io.github.swagger2markup:swagger2markup:1.3.3'
  asciidoctor 'org.asciidoctor:asciidoctorj-pdf:1.5.0-alpha.16'
  testCompile 'org.springframework.boot:spring-boot-starter-test'
  testCompile 'org.springframework.restdocs:spring-restdocs-mockmvc'
}

test {
  systemProperty 'io.springfox.staticdocs.outputDir', swaggerOutputDir
}

convertSwagger2markup {
  dependsOn test
  swaggerInput "${swaggerOutputDir}/swagger.json"
  outputDir asciiDocOutputDir
  config = [
      'swagger2markup.pathsGroupedBy' : 'TAGS',
  ]
}

asciidoctor {
  dependsOn convertSwagger2markup
  sourceDir(asciiDocOutputDir.absolutePath)
  sources {
    include "index.adoc"
  }
  backends = ['pdf']
  attributes = [
      doctype: 'book',
      toc: 'left',
      toclevels: '3',
      numbered: '',
      sectlinks: '',
      sectanchors: '',
      hardbreaks: '',
      generated: asciiDocOutputDir
  ]
}

task genPdf(type: Test, dependsOn: test) {
  include '**/*SwaggerTest.class'
  exclude '**/*'
  dependsOn(asciidoctor)
}

使用asciidoctor-gradle-plugin生成PDF(推薦)

asciidoctor-gradle-plugin也是官方推薦的使用方式。相對前面的方式,使用起來更加簡單,也可以修改配置輸出中文。

1. 引入插件

plugins {
  id 'org.asciidoctor.jvm.pdf' version '2.2.0'
}

2. 編寫測試類生成adoc

與第一中方法不同的是,不需要再將json文件保存到本地了。

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class SwaggerTest {
  @Autowired
  private MockMvc mockMvc;
  @Test
  public void generateAsciiDocsToFile() throws Exception {
    String outputDir = System.getProperty("io.springfox.staticdocs.outputDir");
    MvcResult mvcResult = this.mockMvc.perform(get("/v2/api-docs")
      .accept(MediaType.APPLICATION_JSON))
      .andExpect(status().isOk())
      .andReturn();

    Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder()
      .withMarkupLanguage(MarkupLanguage.ASCIIDOC)
      .withOutputLanguage(Language.ZH)
      .withPathsGroupedBy(GroupBy.TAGS)
      .withGeneratedExamples()
      .withoutInlineSchema()
      .build();

    MockHttpServletResponse response = mvcResult.getResponse();
    String swaggerJson = response.getContentAsString();
    Swagger2MarkupConverter.from(swaggerJson)
      .withConfig(config)
      .build()
      .toFile(Paths.get(outputDir + "/swagger"));
  }
}

有興趣的讀者可以閱讀下toFile方法的源碼,里面對第一種方法生成的4個文件進(jìn)行了整合,這也是不再需要手動編寫index.adoc文件的原因。

3. 配置asciidoctorPdf

ext {
  asciiDocOutputDir = file("${buildDir}/asciidoc")
  // 創(chuàng)建字體與主題的文件夾
  pdfFontsDir = file("${buildDir}/fonts")
  pdfThemesDir = file("${buildDir}/themes")
  swaggerVersion = '2.9.2'
}

pdfThemes {
  local 'basic', {
    styleDir = pdfThemesDir
    // styleName會被程序用于匹配${styleName}-theme.yml,如default-styleName-theme.yml
    styleName = 'default'
  }
}
asciidoctorPdf{
  sourceDir(asciiDocOutputDir.absolutePath)
  sources {
    include "swagger.adoc"
  }
  fontsDir(pdfFontsDir.absolutePath)
  theme("basic")
}

本文字體與主題文件均來自于asciidoctorj-pdf-1.5.0-alpha.18.jar源碼包,其路徑位于:gems/asciidoctorj-pdf-1.5.0-alpha.18/data

4. 復(fù)制并修改default-theme.yml文件配置

為了解決中文無法顯示的問題,首先需要自行下載一個支持中文的字體文件。

修改主題文件,將mplus1p-regular-fallback.ttf替換為自己下載的字體文件的名稱。

M+ 1p Fallback:
 normal: your-font.ttf
 bold: your-font.ttf
 italic: your-font.ttf
 bold_italic: your-font.ttf

由于手動指定了字體文件的路徑,所以除了中文以外,還需要將源碼中的其他字體文件一并復(fù)制到${pdfFontsDir}文件夾。如果不愿意使用官方的字體,也可以考慮將default-theme.yml中其他的字體文件都修改為自己想要的文件。

保持task genPdf不變,再次運(yùn)行即可生成PDF文件,生成的文件默認(rèn)路徑為${build}/docs/asciidocPdf

小結(jié)

asciidoctor-gradle-plugin的方式可以支持配置字體與主題,通過配置不僅規(guī)避了中文無法顯示的問題,同時使用起來也更加簡單。需要注意的是,采用此種方案生成出的文檔會在封面寫有項目的版本號,此版本號為build.gradle中的version,而非SwaggerConfig類中的version。

官方提供了很多配置,可以自行參考官方文檔查看。

build.gradle完整文件參考:

buildscript {
  ext {
    springbootVersion = '2.1.6.RELEASE'
  }
  repositories {
    maven {
      url 'http://maven.aliyun.com/nexus/content/groups/public'
    }
  }
  dependencies {
    classpath "org.springframework.boot:spring-boot-gradle-plugin:${springbootVersion}"
  }
}

plugins {
  id 'org.asciidoctor.jvm.pdf' version '2.2.0'
}

repositories {
  maven {
    url 'http://maven.aliyun.com/nexus/content/groups/public'
  }
}

apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group 'com.jptangchina'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8
targetCompatibility = 1.8

ext {
  asciiDocOutputDir = file("${buildDir}/asciidoc")
  pdfFontsDir = file("${buildDir}/fonts")
  pdfThemesDir = file("${buildDir}/themes")
  swaggerVersion = '2.9.2'
}

dependencies {
  compile 'org.springframework.boot:spring-boot-starter-web'
  compile "io.springfox:springfox-swagger2:${swaggerVersion}"
  compile "io.springfox:springfox-swagger-ui:${swaggerVersion}"
  compile 'io.github.swagger2markup:swagger2markup:1.3.3'
  testCompile 'org.springframework.boot:spring-boot-starter-test'
  testCompile 'org.springframework.restdocs:spring-restdocs-mockmvc'
}

test {
  systemProperty 'io.springfox.staticdocs.outputDir', asciiDocOutputDir
}

pdfThemes {
  local 'basic', {
    styleDir = pdfThemesDir
    styleName = 'default'
  }
}
asciidoctorPdf{
  sourceDir(asciiDocOutputDir.absolutePath)
  sources {
    include "swagger.adoc"
  }
  fontsDir(pdfFontsDir.absolutePath)
  theme("basic")
}

task genPdf(type: Test, dependsOn: test) {
  include '**/*SwaggerTest.class'
  exclude '**/*'
  dependsOn(asciidoctorPdf)
}

參考

  • https://github.com/Swagger2Markup/swagger2markup
  • https://github.com/Swagger2Markup/spring-swagger2markup-demo
  • http://swagger2markup.github.io/swagger2markup/1.3.3

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。

相關(guān)文章

  • IDEA安裝阿里巴巴編碼規(guī)范插件的兩種方式詳解(在線安裝和離線安裝)

    IDEA安裝阿里巴巴編碼規(guī)范插件的兩種方式詳解(在線安裝和離線安裝)

    這篇文章主要介紹了IDEA安裝阿里巴巴編碼規(guī)范插件的兩種方式詳解(在線安裝和離線安裝),本文通過截圖給大家展示的非常詳細(xì),需要的朋友可以參考下
    2021-09-09
  • 在SpringBoot框架中實(shí)現(xiàn)打印響應(yīng)的日志

    在SpringBoot框架中實(shí)現(xiàn)打印響應(yīng)的日志

    這篇文章主要介紹了在SpringBoot框架中實(shí)現(xiàn)打印響應(yīng)的日志,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • Java內(nèi)存結(jié)構(gòu)和數(shù)據(jù)類型

    Java內(nèi)存結(jié)構(gòu)和數(shù)據(jù)類型

    本文重點(diǎn)給大家介紹java內(nèi)存結(jié)構(gòu)和數(shù)據(jù)類型知識,非常不錯,具有參考借鑒價值,需要的朋友參考下
    2016-12-12
  • 面試官問如何啟動Java?線程

    面試官問如何啟動Java?線程

    這篇文章主要介紹了面試官問如何啟動Java?線程,Java?的線程創(chuàng)建和啟動非常簡單,但如果問一個線程是怎么啟動起來的往往并不清楚,甚至不知道為什么啟動時是調(diào)用start(),而不是調(diào)用run()方法呢?下面我們就一起進(jìn)入文章學(xué)習(xí)這個問題吧
    2021-12-12
  • springboot實(shí)現(xiàn)請求參數(shù)驗證的多種方法

    springboot實(shí)現(xiàn)請求參數(shù)驗證的多種方法

    在日常開發(fā)中,我們少不了需要對前端的請求參數(shù)的驗證,Spring提供了多種方法來實(shí)現(xiàn)請求參數(shù)的驗證,文中通過代碼示例給大家講解的非常詳細(xì),我們一起了解一下吧
    2023-11-11
  • Java超詳細(xì)分析繼承與重寫的特點(diǎn)

    Java超詳細(xì)分析繼承與重寫的特點(diǎn)

    繼承是Java面向?qū)ο缶幊讨械囊婚T。繼承是子類繼承父類的特征和行為,或者是繼承父類得方法,使的子類具有父類得的特性和行為。重寫是子類對父類的允許訪問的方法實(shí)行的過程進(jìn)行重新編寫,返回值和形參都不能改變。就是對原本的父類進(jìn)行重新編寫,但是外部接口不能被重寫
    2022-05-05
  • Feign?請求動態(tài)URL方式

    Feign?請求動態(tài)URL方式

    這篇文章主要介紹了Feign?請求動態(tài)URL方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • Java中JWT(JSON?Web?Token)的運(yùn)用具體案例

    Java中JWT(JSON?Web?Token)的運(yùn)用具體案例

    這篇文章主要介紹了Java中JWT(JSON?Web?Token)的運(yùn)用具體案例,JWT(JSON?Web?Token)是一種開放標(biāo)準(zhǔn),用于在網(wǎng)絡(luò)應(yīng)用環(huán)境中安全地傳遞信息,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-11-11
  • Java整數(shù)和字符串相互轉(zhuǎn)化實(shí)例詳解

    Java整數(shù)和字符串相互轉(zhuǎn)化實(shí)例詳解

    這篇文章主要介紹了Java整數(shù)和字符串相互轉(zhuǎn)化實(shí)例詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-02-02
  • Java 內(nèi)部類的定義與范例

    Java 內(nèi)部類的定義與范例

    說起內(nèi)部類這個詞,想必很多人都不陌生,但是又會覺得不熟悉。原因是平時編寫代碼時可能用到的場景不多,用得最多的是在有事件監(jiān)聽的情況下,并且即使用到也很少去總結(jié)內(nèi)部類的用法。今天我們就來一探究竟
    2021-11-11

最新評論

日土县| 弥渡县| 靖远县| 日喀则市| 连云港市| 齐齐哈尔市| 新疆| 桐城市| 常熟市| 赤水市| 沂南县| 什邡市| 慈溪市| 珠海市| 双桥区| 望谟县| 扎鲁特旗| 靖边县| 九龙县| 和政县| 邯郸县| 壶关县| 三河市| 桑日县| 舞阳县| 大悟县| 铁岭市| 庆云县| 车致| 德化县| 沈阳市| 长白| 宣武区| 东明县| 滦平县| 华安县| 柯坪县| 丰城市| 长宁区| 牙克石市| 马关县|