Swagger2創(chuàng)建接口文檔

Spring Boot整合Swagger2

swagger2 是一個(gè)規(guī)范和完整的框架,用于生成、描述、調(diào)用和可視化Restful風(fēng)格的web服務(wù)。

作用:

1、接口的文檔在線自動(dòng)生成

2、功能測(cè)試

Spring Boot整合Swagger2

1.添加Swagger相關(guān)依賴

  </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.9.2</version>
    </dependency>

2.配置Swagger

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.swagger.controller"))
                .paths(PathSelectors.any())
                .build().apiInfo(new ApiInfoBuilder()
                        .description("接口文檔描述信息")
                        .title("xx項(xiàng)目接口文檔")
                        .contact(new Contact("zby","http://www.baidu.com","578499116@qq.com"))
                        .version("v1.0")
                        .license("Apache2.0")
                        .build());

    }
}

寫好測(cè)試類和測(cè)試接口后啟動(dòng)項(xiàng)目,訪問localhost:8080/swagger-ui.html即可看到接口文檔信息


image

也可以在該頁面進(jìn)行接口測(cè)試。

可以在接口中利用swagger2提供的注解對(duì)接口及接口中的方法進(jìn)行描述

@Api(tags = "xxx"):對(duì)接口功能進(jìn)行描述

@ApiOperation(value = "xxx",notes = "xxx"):對(duì)接口中方法的描述

@ApiImplicitParam(name = "xxx",value = "xxx" ):對(duì)參數(shù)進(jìn)行描述

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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