๐Ÿ“– Spring Boot 3์— Swagger(OpenAPI) ๋ถ™์ด๊ณ  ์šด์˜๊นŒ์ง€ ์ฑ™๊ธฐ๊ธฐ

Dev96ยท2025๋…„ 9์›” 11์ผ
post-thumbnail

๋ฐฑ์—”๋“œ API๋ฅผ ๋งŒ๋“ค๋‹ค ๋ณด๋ฉด ํ”„๋ก ํŠธ์—”๋“œ๋‚˜ QA์™€ API ๋ช…์„ธ๋ฅผ ๊ณต์œ ํ•˜๋Š” ์ผ์ด ๋น ์งˆ ์ˆ˜ ์—†๋‹ค. ์˜ˆ์ „์—๋Š” ์—‘์…€์ด๋‚˜ ์œ„ํ‚ค์— ์†์œผ๋กœ ์ ์—ˆ๋Š”๋ฐ ์ง€๊ธˆ์€ Swagger(OpenAPI) ๋กœ ์ฝ”๋“œ์—์„œ ๋ฌธ์„œ๋ฅผ ๋ฐ”๋กœ ๋งŒ๋“ค๊ณ  ์ฝ”๋“œ์™€ ํ•จ๊ป˜ ๊ณ ์ณ ๋‚˜๊ฐ„๋‹ค. Spring Boot 3.4์™€ Java 21๊ณผ Gradle ํ™˜๊ฒฝ์—์„œ Swagger๋ฅผ ๋ถ™์ด๊ณ  ์“ธ ๋งŒํ•˜๊ฒŒ ๋‹ค๋“ฌ๋Š” ๋ฐฉ๋ฒ•์„ ์ •๋ฆฌํ•ด ๋ณธ๋‹ค.

๐Ÿ”ง 1. ์˜์กด์„ฑ ์ถ”๊ฐ€

Spring Boot 3์—์„œ๋Š” springdoc-openapi๋ฅผ ๊ฐ€์žฅ ๋งŽ์ด ์“ด๋‹ค.

// build.gradle
dependencies {
    implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.6.0'
}

โš ๏ธ springdoc์€ Spring Boot ๋ฒ„์ „๋งˆ๋‹ค ๋งž๋Š” ๋ฒ„์ „์ด ๋”ฐ๋กœ ์žˆ๋‹ค. ์˜ฌ๋ฆฌ๊ธฐ ์ „์— springdoc ๋ฌธ์„œ์—์„œ ์ง€๊ธˆ ์“ฐ๋Š” Spring Boot์™€ ๋งž๋Š” ๋ฒ„์ „์ธ์ง€ ํ™•์ธํ•œ๋‹ค.

๐Ÿ“Œ 2. ์–ด๋””์„œ ๋ณด๋‚˜

์˜์กด์„ฑ๋งŒ ๋„ฃ์œผ๋ฉด Swagger UI๊ฐ€ ๋ฐ”๋กœ ์ผœ์ง„๋‹ค.

๋ฌด์—‡๊ธฐ๋ณธ ๊ฒฝ๋กœ
Swagger UIhttp://{ํ˜ธ์ŠคํŠธ}:{ํฌํŠธ}/swagger-ui.html
OpenAPI ๋ฌธ์„œ JSONhttp://{ํ˜ธ์ŠคํŠธ}:{ํฌํŠธ}/v3/api-docs

์ด ๊ธ€์—์„œ๋Š” ์•„๋ž˜ ์„ค์ •์œผ๋กœ ๋ฌธ์„œ ๊ฒฝ๋กœ๋ฅผ /v1/api-docs๋กœ ๋ฐ”๊ฟ”์„œ ์“ด๋‹ค.

โš™๏ธ 3. ์„ค์ • ๋‹ค๋“ฌ๊ธฐ

API ์ •๋ณด

Swagger UI ๋งจ ์œ„์— ๋ณด์ด๋Š” ์ œ๋ชฉ๊ณผ ์„ค๋ช…๊ณผ ๋ฒ„์ „์„ ์ •ํ•œ๋‹ค.

@Configuration
@OpenAPIDefinition(
        info = @Info(
                title = "ํ…Œ์ŠคํŠธ ์ „์šฉ API",
                description = "๊ฐœ์ธ์ ์œผ๋กœ ํ…Œ์ŠคํŠธํ•˜๊ธฐ ์œ„ํ•œ API ๋ฌธ์„œ์ž…๋‹ˆ๋‹ค",
                version = "v1"
        )
)
public class SwaggerConfiguration {
}

ํ™”๋ฉด ์˜ต์…˜๊ณผ ๊ทธ๋ฃน ๋‚˜๋ˆ„๊ธฐ

# application.yml
springdoc:
  default-consumes-media-type: application/json
  default-produces-media-type: application/json
  api-docs:
    path: /v1/api-docs
  swagger-ui:
    path: /swagger-ui.html
    operations-sorter: alpha          # API๋ฅผ ์ด๋ฆ„์ˆœ์œผ๋กœ
    tags-sorter: alpha                # ํƒœ๊ทธ๋ฅผ ์ด๋ฆ„์ˆœ์œผ๋กœ
    doc-expansion: list               # ์ฒ˜์Œ์—” ๋ชฉ๋ก๋งŒ ํŽผ์นœ๋‹ค
    display-request-duration: true    # ์š”์ฒญ์— ๊ฑธ๋ฆฐ ์‹œ๊ฐ„ ํ‘œ์‹œ
    try-it-out-enabled: true          # Try it out์„ ๋ฐ”๋กœ ์ผ ๋‹ค
    disable-swagger-default-url: true
    display-query-params-without-oauth2: true
    defaultModelsExpandDepth: 1
    defaultModelExpandDepth: 2
    defaultModelRendering: model
    show-extensions: true
    show-common-extensions: true
  group-configs:                      # ์‚ฌ์šฉ์ž์™€ ๊ด€๋ฆฌ์ž API๋ฅผ ๋‚˜๋ˆ  ๋ณด์—ฌ ์ค€๋‹ค
    - group: ์‚ฌ์šฉ์ž
      paths-to-match:
        - /v1/member/**
        - /v1/board/**
    - group: ๊ด€๋ฆฌ์ž
      paths-to-match:
        - /v1/admin/**

group-configs๋กœ ๋ฌถ์œผ๋ฉด Swagger UI ์˜ค๋ฅธ์ชฝ ์œ„์—์„œ ์‚ฌ์šฉ์ž์™€ ๊ด€๋ฆฌ์ž ๋ฌธ์„œ๋ฅผ ๊ณจ๋ผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

group-configs๋กœ ์‚ฌ์šฉ์ž์™€ ๊ด€๋ฆฌ์ž ๋ฌธ์„œ๋ฅผ ๋‚˜๋ˆˆ Swagger UI

๐Ÿ”’ ์šด์˜์—์„œ๋Š” ๋ˆ๋‹ค

Swagger๋Š” API ๋ชฉ๋ก๊ณผ ์š”์ฒญ ํ˜•์‹์„ ๊ทธ๋Œ€๋กœ ๋ณด์—ฌ ์ค€๋‹ค. ํšŒ์‚ฌ ๋ณด์•ˆ ์ •์ฑ…์— ๋”ฐ๋ผ ์šด์˜์—์„œ๋Š” ๋„๊ฑฐ๋‚˜ ์ ‘๊ทผ์„ ๋ง‰๋Š” ๊ฒŒ ์•ˆ์ „ํ•˜๋‹ค. ๊ธฐ๋ณธ์€ ๊บผ ๋‘๊ณ  ํ•„์š”ํ•œ ํ”„๋กœํ•„์—์„œ๋งŒ ์ผœ๋Š” ๋ฐฉ๋ฒ•์ด ๊น”๋”ํ•˜๋‹ค.

# application.yml ๊ธฐ๋ณธ์€ ๊บผ ๋‘”๋‹ค
springdoc:
  api-docs:
    enabled: false
  swagger-ui:
    enabled: false
# application-dev.yml ๊ฐœ๋ฐœ์—์„œ๋งŒ ์ผ ๋‹ค
springdoc:
  api-docs:
    enabled: true
  swagger-ui:
    enabled: true

๐Ÿ›  4. Controller์— ๋ฌธ์„œ ๋ถ™์ด๊ธฐ

/**
 * ์‚ฌ์šฉ์ž ์ƒ์„ฑ API
 */
@Operation(
        summary = "์‚ฌ์šฉ์ž ์ƒ์„ฑ",
        requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
                required = true,
                content = @Content(
                        mediaType = "application/json",
                        schema = @Schema(implementation = CreateMemberRequest.class)
                )
        ),
        responses = {
                @ApiResponse(
                        responseCode = "201",
                        description = "์‚ฌ์šฉ์ž ์ƒ์„ฑ ์™„๋ฃŒ",
                        content = @Content(
                                mediaType = "application/json",
                                schema = @Schema(implementation = CommonResponse.class)
                        )
                )
        }
)
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public CommonResponse<CreateMemberResponse> createMember(@Valid @RequestBody CreateMemberRequest request) {
    Member member = memberWebMapper.toDomain(request);
    Member created = createMemberUseCase.createMember(member);
    CreateMemberResponse response = memberWebMapper.toCreateMemberResponse(created);
    return new CommonResponse<>("์‚ฌ์šฉ์ž ์ƒ์„ฑ์ด ์ •์ƒ์ ์œผ๋กœ ์ฒ˜๋ฆฌ๋์Šต๋‹ˆ๋‹ค", response);
}

๐Ÿ‘‰ @RequestBody๋Š” Spring ๊ฒƒ๊ณผ ์ด๋ฆ„์ด ๊ฐ™์•„์„œ Swagger ์ชฝ์€ io.swagger.v3.oas.annotations.parameters.RequestBody๋กœ ์ „์ฒด ์ด๋ฆ„์„ ์ ๋Š”๋‹ค.

๐Ÿ‘‰ 5. ํ”„๋ก ํŠธ์—”๋“œ์™€ ์ด์–ด ์“ฐ๊ธฐ

OpenAPI ๋ฌธ์„œ JSON์ด ์žˆ์œผ๋ฉด ํ”„๋ก ํŠธ์—”๋“œ๊ฐ€ API ์ฝ”๋“œ๋ฅผ ์†์œผ๋กœ ์งœ์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

Spring Boot  โ†’  /v1/api-docs (OpenAPI JSON)  โ†’  ์ฝ”๋“œ ์ƒ์„ฑ๊ธฐ  โ†’  ํ”„๋ก ํŠธ์—”๋“œ ์ฝ”๋“œ

OpenAPI Generator๋‚˜ orval์ด๋‚˜ swagger-typescript-api ๊ฐ™์€ ๋„๊ตฌ๊ฐ€ ์ด JSON์„ ์ฝ๊ณ  ์•„๋ž˜๋ฅผ ๋งŒ๋“ค์–ด ์ค€๋‹ค.

  • ์š”์ฒญ๊ณผ ์‘๋‹ต ํƒ€์ž… ์ •์˜
  • API ํ˜ธ์ถœ ํ•จ์ˆ˜
  • React Query๋‚˜ SWR ํ›…

๐ŸŽฏ ๋งˆ๋ฌด๋ฆฌ

Swagger(OpenAPI)๋Š” API๋ฅผ ๋งŒ๋“ค๊ณ  ์šด์˜ํ•˜๋ฉด์„œ ํ”„๋ก ํŠธ์—”๋“œ์™€ QA์™€ ํ•จ๊ป˜ ์ผํ•˜๊ธฐ ํŽธํ•˜๊ฒŒ ํ•ด ์ฃผ๋Š” ๋„๊ตฌ๋‹ค. Spring Boot์—์„œ๋Š” springdoc-openapi๋กœ ์‰ฝ๊ฒŒ ๋ถ™์ผ ์ˆ˜ ์žˆ๊ณ  ์šด์˜ ๋…ธ์ถœ๊ณผ ํ”„๋กœํ•„ ๋ถ„๋ฆฌ์™€ ๊ทธ๋ฃน ๋‚˜๋ˆ„๊ธฐ๊นŒ์ง€ ์ฑ™๊ธฐ๋ฉด ์‹ค๋ฌด์—์„œ ๋ฐ”๋กœ ์“ธ ์ˆ˜ ์žˆ๋‹ค.

profile
์™œ ํ•„์š”ํ•œ์ง€๋ถ€ํ„ฐ ๋ฌป๋Š” ๋ฐฑ์—”๋“œ ๊ฐœ๋ฐœ์ž์ž…๋‹ˆ๋‹ค. ์‹ค๋ฌด์—์„œ ๋ถ€๋”ชํžˆ๋ฉฐ ๋ฐฐ์šด ๊ฒƒ์„ ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€