Programming/Spring | Spring-Boot

[Spring Boot] Swagger 3.0 적용하기

seandoesdev 2023. 12. 14. 19:36

Swagger란

Rest API를 문서화하여 관리 및 테스트할 수 있도록 도와주는 프레임워크이다.

 

환경설정

java 11

spring boot 2.7.17

swagger : springfox 3.0

 

Swagger 적용

1. 라이브러리 추가

buil.gradle에 아래의 2개 라이브러리 추가한다. 

implementation 'io.springfox:springfox-boot-starter:3.0.0'
implementation 'io.springfox:springfox-swagger-ui:3.0.0'

 

2. SwaggerConfig 파일 생성

@Configuration
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 ApiInfoBuilder()
                .title("Swagger 문서 제목")
                .description("Swagger 문서 설명")
                .version("1.0")
                .build();
    }
}

 

3. application.yml 파일 설정

spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

 

아래 주소로 접근하면 Swagger Ui를 확인할 수 있다.

http://localhost:8080/swagger-ui

 

만일 context-path가 지정되어 있는 경우 8080/{context-path명}/swagger-ui로 접근하면 정상적으로 동작하는 것을 확인 할 수  있을 것이다.