溫馨提示×

springboot集成swagger的方法是什么

小億
113
2024-01-24 18:27:48
欄目: 編程語言

Spring Boot集成Swagger的方法有以下幾個步驟:

  1. 引入Swagger相關依賴:在pom.xml文件中添加以下依賴:
<dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-boot-starter</artifactId>
   <version>3.0.0</version>
</dependency>
  1. 創建Swagger配置類:創建一個Swagger配置類,用于配置Swagger的相關信息和規則。示例代碼如下:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket api() { 
        return new Docket(DocumentationType.SWAGGER_2)  
          .select()                                  
          .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))              
          .paths(PathSelectors.any())                          
          .build();
    }
}

在上述代碼中,basePackage方法指定了需要生成API文檔的包路徑,可以根據實際情況進行修改。

  1. 啟用Swagger:在Spring Boot應用主類上使用@EnableSwagger2注解啟用Swagger。示例代碼如下:
@SpringBootApplication
@EnableSwagger2
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}
  1. 訪問Swagger UI:啟動Spring Boot應用后,可以通過訪問http://localhost:8080/swagger-ui.html來查看生成的API文檔。

注意:上述示例中使用的是Swagger 2.x版本,如果使用的是Swagger 3.x版本,需要相應地調整依賴和配置類。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女