1. 程式人生 > 實用技巧 >基於SpringBoot整合swagger的基本使用

基於SpringBoot整合swagger的基本使用

1.引入依賴(使用的3.0版本已2.x的版本有所區別)

參考於:https://blog.csdn.net/dyc87112/article/details/107424187?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.channel_param

        <
dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency>

2.編寫swagger配置類

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    //建立一個Docket的物件,相當於是swagger的一個例項
@Bean public Docket docket(){ return new Docket(DocumentationType.OAS_30) .apiInfo(apiInfo()); } //配置相關的api資訊 private ApiInfo apiInfo(){ Contact contact=new Contact("yzy","https://www.cnblogs.com/shouyaya/","[email protected]"); return new ApiInfo(
"yzy的swaggerAPI文件", "第一個swagger程式", "1.0", "urn:tos", contact, "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList<>()); } }

3.啟動應用!訪問swagger頁面:http://localhost:8080/swagger-ui/index.html


注意:

  1. 這次更新,移除了原來預設的swagger頁面路徑:http://host/context-path/swagger-ui.html,新增了兩個可訪問路徑:http://host/context-path/swagger-ui/index.htmlhttp://host/context-path/swagger-ui/
  2. 通過調整日誌級別,還可以看到新版本的swagger文件介面也有新增,除了以前老版本的文件介面/v2/api-docs之外,還多了一個新版本的/v3/api-docs介面。