
๋ฐฑ์๋ API๋ฅผ ๋ง๋ค๋ค ๋ณด๋ฉด ํ๋ก ํธ์๋๋ QA์ API ๋ช ์ธ๋ฅผ ๊ณต์ ํ๋ ์ผ์ด ๋น ์ง ์ ์๋ค. ์์ ์๋ ์์ ์ด๋ ์ํค์ ์์ผ๋ก ์ ์๋๋ฐ ์ง๊ธ์ Swagger(OpenAPI) ๋ก ์ฝ๋์์ ๋ฌธ์๋ฅผ ๋ฐ๋ก ๋ง๋ค๊ณ ์ฝ๋์ ํจ๊ป ๊ณ ์ณ ๋๊ฐ๋ค. Spring Boot 3.4์ Java 21๊ณผ Gradle ํ๊ฒฝ์์ Swagger๋ฅผ ๋ถ์ด๊ณ ์ธ ๋งํ๊ฒ ๋ค๋ฌ๋ ๋ฐฉ๋ฒ์ ์ ๋ฆฌํด ๋ณธ๋ค.
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์ ๋ง๋ ๋ฒ์ ์ธ์ง ํ์ธํ๋ค.
์์กด์ฑ๋ง ๋ฃ์ผ๋ฉด Swagger UI๊ฐ ๋ฐ๋ก ์ผ์ง๋ค.
| ๋ฌด์ | ๊ธฐ๋ณธ ๊ฒฝ๋ก |
|---|---|
| Swagger UI | http://{ํธ์คํธ}:{ํฌํธ}/swagger-ui.html |
| OpenAPI ๋ฌธ์ JSON | http://{ํธ์คํธ}:{ํฌํธ}/v3/api-docs |
์ด ๊ธ์์๋ ์๋ ์ค์ ์ผ๋ก ๋ฌธ์ ๊ฒฝ๋ก๋ฅผ /v1/api-docs๋ก ๋ฐ๊ฟ์ ์ด๋ค.
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 ์ค๋ฅธ์ชฝ ์์์ ์ฌ์ฉ์์ ๊ด๋ฆฌ์ ๋ฌธ์๋ฅผ ๊ณจ๋ผ ๋ณผ ์ ์๋ค.

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
/**
* ์ฌ์ฉ์ ์์ฑ 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๋ก ์ ์ฒด ์ด๋ฆ์ ์ ๋๋ค.
OpenAPI ๋ฌธ์ JSON์ด ์์ผ๋ฉด ํ๋ก ํธ์๋๊ฐ API ์ฝ๋๋ฅผ ์์ผ๋ก ์ง์ง ์์๋ ๋๋ค.
Spring Boot โ /v1/api-docs (OpenAPI JSON) โ ์ฝ๋ ์์ฑ๊ธฐ โ ํ๋ก ํธ์๋ ์ฝ๋
OpenAPI Generator๋ orval์ด๋ swagger-typescript-api ๊ฐ์ ๋๊ตฌ๊ฐ ์ด JSON์ ์ฝ๊ณ ์๋๋ฅผ ๋ง๋ค์ด ์ค๋ค.
Swagger(OpenAPI)๋ API๋ฅผ ๋ง๋ค๊ณ ์ด์ํ๋ฉด์ ํ๋ก ํธ์๋์ QA์ ํจ๊ป ์ผํ๊ธฐ ํธํ๊ฒ ํด ์ฃผ๋ ๋๊ตฌ๋ค. Spring Boot์์๋
springdoc-openapi๋ก ์ฝ๊ฒ ๋ถ์ผ ์ ์๊ณ ์ด์ ๋ ธ์ถ๊ณผ ํ๋กํ ๋ถ๋ฆฌ์ ๊ทธ๋ฃน ๋๋๊ธฐ๊น์ง ์ฑ๊ธฐ๋ฉด ์ค๋ฌด์์ ๋ฐ๋ก ์ธ ์ ์๋ค.