Swagger

Swagger란?

1.의존성 추가

build.gradle에 아래 의존성들을 추가한다.

implementation 'io.springfox:springfox-swagger2:2.9.2'
implementation 'io.springfox:springfox-swagger-ui:2.9.2'

2. 프로젝트에 Swagger 설정 Bean을 등록

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                // 스웨거가 RestController를 전부 스캔을 한다.
                .apis(RequestHandlerSelectors.any())
                // basePackage => 어디를 범위로 스캔을 할 것인지 작성
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {

        return new ApiInfoBuilder()
                .title("API 서비스")
                .description("API 서비스 입니다")
                .version("0.1.0")
                .license("LICENSE")
                .licenseUrl("")
                .build();
    }
}

3. ApiController에 Swagger 관련 어노테이션 작성

4. yml 파일에 추가

Failed to start bean ‘documentationPluginsBootstrapper’; nested exception is java.lang.NullPointerException” 에러발생

Spring boot 2.6버전 이후에 spring.mvc.pathmatch.matching-strategy 값이 
ant_apth_matcher에서 path_pattern_parser로 변경되면서 몇몇 라이브러리(swagger포함)에 오류가 발생한다고 한다.

  spring:
     mvc:
     pathmatch:
        matching-strategy: ant_path_matcher

5.Swagger-ui 확인

브라우저를 켜고 localhost:8080/swagger-ui.html 접속