1. 程式人生 > 其它 >3. SpringBoot整合Swagger3.0

3. SpringBoot整合Swagger3.0

SpringBoot整合Swagger3.0

  1. 引入依賴

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-boot-starter</artifactId>
        <version>3.0.0</version>
    </dependency>
    
  2. 載入配置

    @Configuration
    public class Swagger3Config {
        @Bean
        public Docket createRestApi() {
            return new Docket(DocumentationType.OAS_30)
                    .apiInfo(apiInfo())
                    .select()
    //                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                    //為當前包路徑
                    .apis(RequestHandlerSelectors.basePackage("控制層包"))
                    .paths(PathSelectors.any())
                    .build();
        }
    
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title("Swagger3介面文件")
                    .description("更多請諮詢服務開發者Ray。")
                    .contact(new Contact("Ray。", "http://www.ruiyeclub.cn", "[email protected]"))
                    .version("1.0")
                    .build();
        }
    }
    
  3. Application類上加註解

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import springfox.documentation.oas.annotations.EnableOpenApi;
    
    @EnableOpenApi
    @SpringBootApplication
    public class DemoApplication {
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class,args);
        }
    }
    
  4. 訪問路徑:http://localhost:8080/swagger-ui/