• Home
  • About
    • lahuman photo

      lahuman

      열심히 사는 아저씨

    • Learn More
    • Facebook
    • LinkedIn
    • Github
  • Posts
    • All Posts
    • All Tags
  • Projects

[kotlin + Spring]swagger 를 설정하자!

26 Jan 2024

Reading time ~1 minute

springdoc-openapi v2.3.0

springBoot 3.x 부터는 springdoc-openapi v2.3.0 모듈을 사용해야 합니다. 이전 버젼은 springdoc-openapi v1.7.0을 이용해야 합니다.

설치는 build.gradle.kts 에 아래 디펜던시를 추가 합니다.

implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui:2.3.0")

Swagger 설정은 다음과 같이 단순하게 class를 추가해서 설정할 수 있습니다.

package io.lahuman.config

import io.swagger.v3.oas.annotations.OpenAPIDefinition
import io.swagger.v3.oas.annotations.info.Info
import io.swagger.v3.oas.annotations.servers.Server
import org.springframework.context.annotation.Configuration


@OpenAPIDefinition(
        info = Info(title = "제목",
                description = "설명",
                version = "v1"),
        servers = [
            Server(url = "\${springdoc.swagger-ui.server}")
        ]
)
@Configuration
class SwaggerConfig

추가로 /resource/application.yml에 몇가지 설정을 넣어서 마무리 하면 됩니다.


springdoc:
  default-consumes-media-type: application/json
  default-produces-media-type: application/json
  swagger-ui:
    tags-sorter: alpha
    server: http://localhost:8080/
    path: /swagger-ui.html
server:
  servlet:
    context-path: /
    encoding:
      force: true

실제 swagger 화면을 보기 위한 접속 주소는 http://localhost:8080/swagger-ui.html 으로 접근하면 swagger 화면을 확인 할 수 있습니다.

참고자료

  • springdoc-openapi v2.3.0


kotlinspringswagger Share Tweet +1