Swagger使用及導(dǎo)出

1、依賴

<swagger.version>2.9.2</swagger.version>

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>${swagger.version}</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>${swagger.version}</version>
</dependency>

2、SwaggerConfig

@Configuration
@EnableSwagger2
@Profile({"dev", "test"})
public class Swagger2Config {
    @Autowired
    private SignConfig appKeyConfig;

    @Bean
    public Docket createRestApi() {
        List<Parameter> pars = new ArrayList<>();
        if (appKeyConfig.isSignOn()) {
            //添加header
            ParameterBuilder timestamp = new ParameterBuilder();
            timestamp.name("timestamp").description("時間戳").modelRef(new ModelRef("string")).parameterType("header").required(true).build();

            ParameterBuilder sign = new ParameterBuilder();
            sign.name("sign").description("簽名").modelRef(new ModelRef("string")).parameterType("header").required(true).build();

            ParameterBuilder appId = new ParameterBuilder();
            appId.name("appId").description("appId").modelRef(new ModelRef("string")).parameterType("header").required(true).build();

            pars.add(timestamp.build());
            pars.add(sign.build());
            pars.add(appId.build());
        }

        Docket docket = new Docket(DocumentationType.SWAGGER_2);

        docket.apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))//這是注意的代碼
                .paths(PathSelectors.any())
                .build().globalOperationParameters(pars);
        return docket;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("xxxxx接口文檔")
                .termsOfServiceUrl("https://www.xxxxx.com")
                .version("1.0")
                .build();
    }
}

3、注解

controller

@Api(tags = "xxxxxxxAPI")

controller 方法

@ApiOperation("xxxxxx接口")

VO、DTO內(nèi)屬性

@ApiModelProperty(value = "id", example = "56449e5fb3a39283af2e1cbe")
private String id;

4、導(dǎo)出html、pdf、markDown

配置pom后:
1、生成markdown、ASCIIDOC,執(zhí)行mvn swagger2markup:convertSwagger2markup
2、生成 html 、pdf,執(zhí)行1后,執(zhí)行mvn generate-resources
3、pdf中文顯示存在問題

<!-- 此插件生成markdown、ASCIIDOC、wiki格式-->
            <!--  執(zhí)行命令 mvn swagger2markup:convertSwagger2markup-->
            <plugin>
                <groupId>io.github.swagger2markup</groupId>
                <artifactId>swagger2markup-maven-plugin</artifactId>
                <version>1.3.7</version>
                <configuration>
                    <!-- api-docs訪問url -->
                    <swaggerInput>http://localhost:8099/food/v2/api-docs</swaggerInput>
                    <!-- 生成為單個文檔,輸出路徑-->
                    <outputFile>src/docs/api</outputFile>
                    <!-- 生成為多個文檔,輸出路徑 -->
                    <!--<outputDir>src/docs/</outputDir>-->
                    <config>
                        <!-- wiki格式文檔 -->
                        <!--<swagger2markup.markupLanguage>CONFLUENCE_MARKUP</swagger2markup.markupLanguage>-->
                        <!-- ascii格式文檔 -->
                        <swagger2markup.markupLanguage>ASCIIDOC</swagger2markup.markupLanguage>
                        <!-- markdown格式文檔 -->
                        <!--<swagger2markup.markupLanguage>MARKDOWN</swagger2markup.markupLanguage>-->
                        <swagger2markup.pathsGroupedBy>TAGS</swagger2markup.pathsGroupedBy>
                    </config>
                </configuration>
            </plugin>
            <!--此插件生成HTML和PDF-->
            <!-- 執(zhí)行命令 mvn generate-resources -->
            <plugin>
                <groupId>org.asciidoctor</groupId>
                <artifactId>asciidoctor-maven-plugin</artifactId>
                <version>2.1.0</version>
                <!-- Include Asciidoctor PDF for pdf generation -->
                <dependencies>
                    <dependency>
                        <groupId>org.asciidoctor</groupId>
                        <artifactId>asciidoctorj-pdf</artifactId>
                        <version>1.5.4</version>
                    </dependency>
                    <dependency>
                        <groupId>org.jruby</groupId>
                        <artifactId>jruby-complete</artifactId>
                        <version>9.2.17.0</version>
                    </dependency>
                </dependencies>
                <!-- Configure generic document generation settings -->
                <configuration>
                    <sourceDirectory>src/docs</sourceDirectory>
                    <!-- <sourceHighlighter>coderay</sourceHighlighter>-->
                    <attributes>
                        <toc>left</toc>
                    </attributes>
                </configuration>
                <!-- Since each execution can only handle one backend, run
                     separate executions for each desired output type -->
                <executions>
                    <execution>
                        <id>output-pdf</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>process-asciidoc</goal>
                        </goals>
                        <configuration>
                            <backend>pdf</backend>
                            <outputDirectory>src/docs/pdf/</outputDirectory>
                        </configuration>
                    </execution>
                    <execution>
                        <id>output-html</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>process-asciidoc</goal>
                        </goals>
                        <configuration>
                            <backend>html5</backend>
                            <outputDirectory>src/docs/html/</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容