Springboot 3.x에 Swagger를 적용시켜보자!

준커·2023년 1월 17일
16

Spring

목록 보기
1/3
post-thumbnail

Springboot 버전 및 테스트 환경

  • 해당 글에서는 Springboot 3.0.1을 기준으로 작성되었습니다.

  • SpringSecurity는 적용하지 않았다는 것을 알려드립니다.

1번째 시도 (실패)

Spring boot 3.0.1 + Springfox Swagger 2.9.2

dependencies {
	
	implementation 'io.springfox:springfox-swagger2:2.9.2'
	implementation 'io.springfox:springfox-swagger-ui:2.9.2'
    
}

1번째 시도에는 버전 생각 1도 없이 많이들 쓴다는 Swagger 2.9.2로 적용하려고 시도하였다.

그러자 바로 3.0.0 버전으로 Upgrade 하라는 옵션이 떠서 2.9.2버전은 깔끔하게 버리고 3.0.0부터 적용에 시도하였다.

2번째 시도 (실패)

Spring boot 3.0.1 + Springfox Swagger 3.0.0

dependencies {
	
	implementation 'io.springfox:springfox-swagger2:3.0.0'
	implementation 'io.springfox:springfox-swagger-ui:3.0.0'
    
}

하지만 Swagger 3.0.0부턴 1개의 종속성만 추가해도 된다.

dependencies {
	
	implementation 'io.springfox:springfox-boot-starter:3.0.0'
    
}

이후 버전에 알맞게 접속하여 테스트해 보면 된다.
Swagger 2.x.x까지는 localhost:8080/swagger-ui.html이였지만.
Swagger 3.0.0부터는 localhost:8080/swagger-ui/index.html로 변경되었기 때문에 이 부분 신경 써서 접속하면 된다.


결과는 Whitelabel Error.

SwaggerConfig 추가

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

    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("test")
                .description("description")
                .version("1.0.0")
                .build();
    }
}

혹시 몰라 SwaggerConfig를 추가해 보았지만 결과는 동일하였다.

어노테이션의 문제일까??

SwaggerConfig에 @Configuration만 붙였는데, @EnableSwagger2를 붙여보라는 말이 있었고, Swagger 3.0.0부턴 @EnableWebMvc로 대체되었다는 말이 있어서 두 가지 경우를 모두 해보았다.

1. @Configuration + @EnableSwagger2


에러 발생..
@Configuration를 지우면 해결된다는 글을 보아서 지워보았다.

2. @EnableSwagger2


결과는 Whitelabel Error.

3. @Configuration + @EnableWebMvc


결과는 Whitelabel Error.

4. @EnableWebMvc


결과는 Whitelabel Error.

3번째 시도 (성공)

Spring boot 3.0.1 + Springdoc Swagger 3.0.0

구글링을 열심히 해보던 도중 Springfox대신 Springdoc를 사용해 보라는 글을 보아 적용해 보았다.

공식문서: https://springdoc.org

1. springdoc-openapi-ui 사용 (실패)

Springdoc 공식 문서를 보며 따라 해보았다.

dependencies {
	
	implementation 'org.springdoc:springdoc-openapi-ui:1.6.14'
    
}


여전히 Whitelabel Error.

2. springdoc-openapi-starter-webmvc-ui 사용 (성공)

다시 구글링을 하던 와중 구세주가 등장했다.

https://stackoverflow.com/questions/74614369/how-to-run-swagger-3-on-spring-boot-3

다른 종속성으로 주입하면 해결이 가능하다는 것을 보고 시도해 보았다.

dependencies {
	
	implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.2'
    
}


드디어 성공이다!!

SwaggerConfig는 추가하지 않아도 작동이 된다.

추가적인 기능 및 적용 방법은 공식 문서를 참조해 보자!

공식문서: https://springdoc.org/v2/

결론

Springboot 3.0.1 버전에서 Swagger를 사용하고 싶다면 다음 종속성을 사용해 보자!

implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.2'
profile
학부생일뿐

9개의 댓글

comment-user-thumbnail
2023년 4월 22일

감사합니다,,👊🏻

답글 달기
comment-user-thumbnail
2023년 4월 27일

kjgi73k 그는신인가?kjgi73k 그는신인가?kjgi73k 그는신인가?kjgi73k 그는신인가?kjgi73k 그는신인가?kjgi73k 그는신인가?kjgi73k 그는신인가?

답글 달기
comment-user-thumbnail
2023년 6월 12일

추천 500개 드립니다

답글 달기
comment-user-thumbnail
2023년 6월 30일

감사합니다!!!!👍

답글 달기
comment-user-thumbnail
2023년 11월 8일

저만 자꾸 실패하는줄 알았는데.. 위로가 됩니다. 감사합니다~~

답글 달기
comment-user-thumbnail
2023년 12월 11일

감사합니다. 덕분에 해결했습니다.

답글 달기
comment-user-thumbnail
2024년 1월 3일

감사합니다 😭😭

답글 달기
comment-user-thumbnail
2024년 1월 15일

당신은 내 영웅입니다

답글 달기
comment-user-thumbnail
2024년 1월 18일

감사합니다 ㅠㅠㅠㅠ

답글 달기