[Spring Boot] Swagger 2 설정하기

    Rest Api 문서화를 위한 Spring Boot Swagger 2 설정하기

    1. swagger dependency 추가

    Maven 설정

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

    Gradle 설정

    implementation group: 'io.springfox', name: 'springfox-swagger2', version: '3.0.0'
    implementation group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.9.2'

    2. config 설정

    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {
        @Bean
        public Docket api() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .select()
                    .apis(RequestHandlerSelectors.any())
                    .paths(PathSelectors.any())
                    .build();
        }
    
    }

    3. Custom 정보 추가

    선택적으로 ApiInfo 를 추가 할 수 있다.

    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {
        @Bean
        public Docket api() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .select()
                    .apis(RequestHandlerSelectors.any())
                    .paths(PathSelectors.any())
                    .build()
                    .apiInfo(apiInfo());
        }
    
        private ApiInfo apiInfo() {
            return new ApiInfo(
                    "Sample REST API",
                    "This is sample API.",
                    "V1",
                    "Terms of service",
                    new Contact("administrator", "www.example.com", "administrator@email.com"),
                    "License of API", "www.example.com", Collections.emptyList());
        }
    }

    4. swagger 접속

    http://localhost:8080/swagger-ui.html 접속

    참고

    반응형

    댓글

    Designed by JB FACTORY