본문 바로가기

Development/Spring

[Spring] Springboot Swagger 3 적용하기 (Springdoc-OpenAPI3)

Springdoc-OpenAPI3

springdoc-openapi 자바 라이브러리는 스프링 부트 프로젝트를 사용하여 API 문서화를 자동화하는 데 도움을 줍니다.

springdoc-openapi는 런타임에서 애플리케이션을 검사하여 스프링 구성, 클래스 구조 및 여러 주석을 기반으로 API 의미를 추론하는 방식으로 작동합니다.

Springdoc-openAPI의 모듈 구조

springdoc-openAPI 라이브러리는 다음 기능을 제공합니다.

  • OpenAPI 3
  • Spring-boot v3 (Java 17 & Jakarta EE 9)
  • JSR-303, specifically for @NotNull, @Min, @Max, and @Size.
  • Swagger-ui
  • OAuth 2
  • GraalVM native images

Swagger-UI

Swagger UI는 API 문서를 시각적으로 표현하고, 사용자가 API를 테스트할 수 있는 페이지를 제공합니다.

Maven 설치법

pom.xml에 다음 코드를 추가합니다.

   <dependency>
      <groupId>org.springdoc</groupId>
      <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
      <version>2.3.0</version>
   </dependency>

Gradle 설치법

build.gradle 파일에 dependencies 에서 아래 코드를 추가해줍니다.

implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.3.0'

꼭 Reload All Gradle Projects를 해줍니다.. 

Swagger 접속하기

Swagger UI 페이지는 다음 URL에서 접속할 수 있습니다.

  • http://server:port/context-path/swagger-ui.html

다음 URL에서 json 형식으로 조회할 수 있습니다.

  • http://server:port/context-path/v3/api-docs

Example

  • http://localhost:8080/swagger-ui/index.html
  • http://localhost:8080/v3/api-docs
  • http://localhost:8080/swagger-ui.html

공식 문서

https://springdoc.org/

 

OpenAPI 3 Library for spring-boot

Library for OpenAPI 3 with spring boot projects. Is based on swagger-ui, to display the OpenAPI description.Generates automatically the OpenAPI file.

springdoc.org