SpringBoot+Swagger2+Asciidoctor輸出靜態(tài)文檔

1. 軟件依賴

Spring Boot
Swagger2
Swagger2markup
asciidoctor-maven-plugin

2. SpringBoot配置Swagger2

2.1 引入Swagger2相關(guān)依賴

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.8.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.8.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-staticdocs</artifactId>
            <version>2.6.1</version>
        </dependency>
        <dependency>
            <groupId>io.github.swagger2markup</groupId>
            <artifactId>swagger2markup</artifactId>
            <version>1.3.1</version>
        </dependency>

2.1 編寫(xiě)SwaggerConfig配置類

@Configuration
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.xxx.xxx.controller"))
                .paths(PathSelectors.any())
                .build();
    }
    
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("API文檔")
                .description("API使用說(shuō)明")
                .termsOfServiceUrl("http://localhost:8088")
                .version("1.0")
                .build();
    }
}
@SpringBootApplication
@EnableSwagger2
public class MyApplication {

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

2.2 在controller的方法上添加swagger注解

    @ApiOperation(value = "刪除指定ID標(biāo)記")
    @ApiImplicitParam(name = "id", value = "標(biāo)記ID", required = true, dataType = "string")
    @RequestMapping(value = "/mark/{id}", method = RequestMethod.DELETE)
    public void delete(@PathVariable String id) {
        markService.delete(id);
    }
    
    @ApiOperation(value = "查詢指定ID標(biāo)記")
    @ApiImplicitParam(name = "id", value = "標(biāo)記ID", required = true, dataType = "string")
    @RequestMapping(value = "/mark/{id}", method = RequestMethod.GET)
    public TblMark getById(@PathVariable String id) {
        return markService.getById(id);
    }
    
    @ApiOperation(value = "更新指定ID標(biāo)記")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "標(biāo)記ID", required = true, dataType = "string"),
        @ApiImplicitParam(name = "mark", value = "標(biāo)記實(shí)體TblMark", required = true, dataType = "TblMark")
    })
    @RequestMapping(value = "/mark/{id}", method = RequestMethod.PUT)
    public TblMark update(@PathVariable String id,@RequestBody TblMark mark) {
        return markService.update(id, mark);
    }

3. 編寫(xiě)測(cè)試類生成asciidoc

    @Test
    public void generateAsciiDocs() throws MalformedURLException {
        Swagger2MarkupConfig config = new Swagger2MarkupConfigBuilder().withMarkupLanguage(MarkupLanguage.ASCIIDOC)
                .build();
        Swagger2MarkupConverter.from(new URL("http://localhost:8088/v2/api-docs")).withConfig(config).build()
                .toFile(Paths.get("src/docs/asciidoc/generated/all"));
    }

3. 使用asciidoctor-maven-plugin生成HTML文檔

3.1 配置asciidoctor-maven-plugin插件

3.1.1 添加插件倉(cāng)庫(kù)
    <pluginRepositories>
        <pluginRepository>
            <id>jcenter-snapshots</id>
            <name>jcenter</name>
            <url>http://oss.jfrog.org/artifactory/oss-snapshot-local/</url>
        </pluginRepository>
        <pluginRepository>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
            <id>jcenter-releases</id>
            <name>jcenter</name>
            <url>http://jcenter.bintray.com</url>
        </pluginRepository>
    </pluginRepositories>
3.1.2 配置插件的sourceDirectory屬性為asciidoc文檔地址
                <plugin>
                    <groupId>org.asciidoctor</groupId>
                    <artifactId>asciidoctor-maven-plugin</artifactId>
                    <version>${asciidoctor.maven.plugin.version}</version>
                    <dependencies>
                        <!-- Comment this section to use the default jruby artifact provided 
                            by the plugin -->
                        <dependency>
                            <groupId>org.jruby</groupId>
                            <artifactId>jruby-complete</artifactId>
                            <version>${jruby.version}</version>
                        </dependency>
                        <!-- Comment this section to use the default AsciidoctorJ artifact 
                            provided by the plugin -->
                        <dependency>
                            <groupId>org.asciidoctor</groupId>
                            <artifactId>asciidoctorj</artifactId>
                            <version>${asciidoctorj.version}</version>
                        </dependency>
                    </dependencies>
                    <configuration>
                        <sourceDirectory>src/docs/asciidoc/generated</sourceDirectory>
                        <attributes>
                            <endpoint-url>http://example.org</endpoint-url>
                            <sourcedir>${project.build.sourceDirectory}</sourcedir>
                            <project-version>${project.version}</project-version>
                        </attributes>
                    </configuration>
                    <executions>
                        <execution>
                            <id>asciidoc-to-html</id>
                            <phase>generate-resources</phase>
                            <goals>
                                <goal>process-asciidoc</goal>
                            </goals>
                            <configuration>
                                <backend>html5</backend>
                                <sourceHighlighter>coderay</sourceHighlighter>
                                <attributes>
                                    <imagesdir>./images</imagesdir>
                                    <toc>left</toc>
                                    <icons>font</icons>
                                    <sectanchors>true</sectanchors>
                                    <idprefix />
                                    <idseparator>-</idseparator>
                                    <docinfo1>true</docinfo1>
                                </attributes>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>

4. 生成文檔

4.1 運(yùn)行asciidoc測(cè)試類

4.2 運(yùn)行mvn generate-resources朱转,在target/generated-docs下生成html文檔

5.注意

5.1 POM文件中的<pluginManagement>標(biāo)簽會(huì)讓其中的插件不執(zhí)行,導(dǎo)致錯(cuò)誤

5.2 <execution>標(biāo)簽的錯(cuò)誤可在eclipse中選擇quickFix诱鞠,進(jìn)行忽略

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末傍药,一起剝皮案震驚了整個(gè)濱河市同窘,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖独柑,帶你破解...
    沈念sama閱讀 217,542評(píng)論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件筛武,死亡現(xiàn)場(chǎng)離奇詭異缝其,居然都是意外死亡挎塌,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,822評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門(mén)内边,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)榴都,“玉大人,你說(shuō)我怎么就攤上這事漠其∽旄撸” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 163,912評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵和屎,是天一觀的道長(zhǎng)拴驮。 經(jīng)常有香客問(wèn)我,道長(zhǎng)眶俩,這世上最難降的妖魔是什么莹汤? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,449評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮颠印,結(jié)果婚禮上纲岭,老公的妹妹穿的比我還像新娘。我一直安慰自己线罕,他們只是感情好止潮,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,500評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著钞楼,像睡著了一般喇闸。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上询件,一...
    開(kāi)封第一講書(shū)人閱讀 51,370評(píng)論 1 302
  • 那天燃乍,我揣著相機(jī)與錄音,去河邊找鬼宛琅。 笑死刻蟹,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的嘿辟。 我是一名探鬼主播舆瘪,決...
    沈念sama閱讀 40,193評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼红伦!你這毒婦竟也來(lái)了英古?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,074評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤昙读,失蹤者是張志新(化名)和其女友劉穎召调,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,505評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡某残,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,722評(píng)論 3 335
  • 正文 我和宋清朗相戀三年国撵,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片玻墅。...
    茶點(diǎn)故事閱讀 39,841評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡介牙,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出澳厢,到底是詐尸還是另有隱情环础,我是刑警寧澤,帶...
    沈念sama閱讀 35,569評(píng)論 5 345
  • 正文 年R本政府宣布剩拢,位于F島的核電站线得,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏徐伐。R本人自食惡果不足惜贯钩,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,168評(píng)論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望办素。 院中可真熱鬧角雷,春花似錦、人聲如沸性穿。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,783評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)需曾。三九已至吗坚,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間呆万,已是汗流浹背商源。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,918評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留谋减,地道東北人炊汹。 一個(gè)月前我還...
    沈念sama閱讀 47,962評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像逃顶,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子充甚,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,781評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容