본문 바로가기

WEB/Spring

[Spring] Swagger 에러(Unable to infer base url. 하얀화면), SpringDoc Swagger

Java1.8, SpringBoot 2.7.17, Maven으로 진행했던 프로젝트를 Spring Boot 3.2, Java 17, Gradle로 바꾸어 복습하고 있었습니다.

Spring에 swagger를 추가하면서 발생한 에러입니다.

Unable to infer base url. This is common when using dynamic servlet registration or when the API is behind an API Gateway. The base url is the root of where all the swagger resources are served. For e.g. if the api is available at http://example.org/api/v2/api-docs then the base url is http://example.org/api/. Please enter the location manually:

 

build.gradle에 다음과 같이 추가해주었고 SwaggerConfig 또한 이전 프로젝트와 같이 구성해놓은 상태였습니다.

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

 

결론적으로 해결책은 springfox가 아닌 SpringDoc을 사용하는것이였습니다.

SpringFox는 2020년 이후로 업데이트가 진행되지 않고 있습니다. 하지만 SpringDoc은 꾸준히 업데이트가 진행되고 있습니다.

 

https://mvnrepository.com/artifact/org.springdoc/springdoc-openapi-starter-webmvc-ui/2.0.2

build.gradle에 추가해줍시다.

implementation 'org.springdoc:springdoc-openapi-ui:1.6.11'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.2'

 

SwaggerConfig또한 간단하게 작성했습니다.

 

 

제대로 나오는것을 확인하였습니다.


SpringDoc Swagger사용 중 기존 Swagger와 Annotation이 달라서 찾아왔습니다....!

 

  • @Api → @Tag
  • @ApiIgnore → @Parameter(hidden = true) or @Operation(hidden = true) or @Hidden
  • @ApiImplicitParam→@Parameter
  • @ApiImplicitParams → @Parameters
  • @ApiModel → @Schema
  • @ApiModelProperty(hidden = true) → @Schema(accessMode = READ_ONLY)
  • @ApiModelProperty → @Schema
  • @ApiOperation(value = "foo", notes = "bar") → @Operation(summary = "foo", description = "bar")
  • @ApiParam → @Parameter
  • @ApiResponse(code = 404, message = "foo") → @ApiResponse(responseCode = "404", description = "foo")

출처 : https://springdoc.org/#migrating-from-springfox