- ๊ณต์ ์ฌ์ดํธ https://swagger.io/
- ์ฐธ๊ณ https://real-dongsoo7.tistory.com/58
- Open Api Specification(OAS)๋ฅผ ์ํ ํ๋ ์์ํฌ
- API๋ค์ด ๊ฐ์ง๊ณ ์๋ ์คํ(spec)์ ๋ช
์ธ, ๊ด๋ฆฌํ ์ ์๋ ํ๋ก์ ํธ
- API ๋ฌธ์ํ๋ฅผ ์ฝ๊ฒ ํ ์ ์๋๋ก ๋์์ค
- ํ๋ผ๋ฏธํฐ๋ฅผ ๋ฃ์ด์ ์ค์ ๋ก ์ด๋ค ์๋ต์ด ์ค๋์ง ํ
์คํธ ๊ฐ๋ฅ
- Swagger๋ง ์ ๋ฌํด์ฃผ๋ฉด API Path์ Request, Response ๊ฐ ๋ฐ ์ ์ฝ ๋ฑ์ ํ๋ฒ์ ์๋ ค์ค ์ ์์
- API ๋์์ธ : Swagger-editor๋ฅผ ํตํด api๋ฅผ ๋ฌธ์ํํ๊ณ ๋น ๋ฅด๊ฒ ๋ช
์ธํ ์ ์์
- Swagger Editor : Swagger ํ์ค์ ๋ฐ๋ฅธ API ์ค๊ณ์/๋ช
์ธ์๋ฅผ ์์ฑํ๊ธฐ ์ํ ์๋ํฐ
- API Development : Swagger-codegen์ ํตํด ์์ฑ๋ ๋ฌธ์๋ฅผ ํตํด SDK๋ฅผ ์์ฑํ์ฌ ๋น๋ ํ๋ก์ธ์ค๋ฅผ ๊ฐ์ํ ํ ์ ์๋๋ก ๋์์ค / ๋ฌธ์๋ฅผ ํตํด ์คํํ๋ฉด ํ๋กํ ํ์
์ฝ๋๋ฅผ ์์ฑ
- Swagger Codegen : Swagger๋ก ์ ์๋๋๋ก ํด๋ผ์ด์ธํธ/์๋ฒ ์ฝ๋๋ฅผ ์์ฑํ๋ CLI ํด
- API Documentation : Swagger-UI๋ฅผ ํตํด ์์ฑ๋ API๋ฅผ ์๊ฐํ
- Swagger UI : Swagger UI๋ Swagger API ๋ช
์ธ์๋ฅผ HTML ํ์์ผ๋ก ํ์ธํ ์ ์๋ ํด
- API Testing : Swagger-Inspector๋ฅผ ํตํด API๋ฅผ ์๊ฐํ ํ๊ณ ๋น ๋ฅธ ํ
์คํ
์ ์งํ
- Standardize : Swagger-hub๋ฅผ ํตํด ๊ฐ์ธ, ํ์๋ค์ด API ์ ๋ณด๋ฅผ ๊ณต์ ํ๋ Hub
1-2. Open API์์ ๊ด๊ณ
- ๊ณต์ ์ฌ์ดํธ > Docs ํญ - ๋ฌธ์ ์์ฑํ๋ ๊ธฐ์ด์ ์ธ ๋ฌธ๋ฒ์ ํ์ธํ ์ ์์
- ๋ํ๋จผํธ ์ต์
์ ์ ํ ํ ์ด๋ (๊ธฐ๋ณธ์ OpenAPI V3.0)
- ์ฌ์ฉ ๋ฐฉ๋ฒ : ๊ณต์ ์ฌ์ดํธ์ Live Demo๋ฅผ ์ด์ฉ / ์๋ํฐ ์์ค๋ฅผ ๋ค์ด ๋ฐ์์ ์ฌ์ฉ
- docker๋ฅผ ์ด์ฉํ์ฌ Swagger-Editor๋ฅผ ์ค์นํ๋ ค๋ฉด ์๋ ๋ช
๋ น์ด ์
๋ ฅ
docker pull swaggerapi/swagger-editor
docker run -d -p 80:8080 swaggerapi/swagger-editor

- ๋ก์ปฌ 80 ํฌํธ๋ฅผ ํตํด Swagger-editor๊ฐ ์คํ๋๊ณ ์์์ ํ์ธ
- ๋ธ๋ผ์ฐ์ ๋ฅผ ํตํด localhost:80 ํฌํธ๋ก ์ ๊ทผ์ ํ๋ฉด Swagger-editor ํ๋ฉด ํ์ธ ๊ฐ๋ฅ
- yaml, JSON ํ์์ผ๋ก ์์ฑ ๊ฐ๋ฅ
- ์๋ ๋ด์ฉ์ Swagger๋ฅผ ์ด์ฉํ๊ธฐ ์ํ ๊ธฐ๋ณธ ์ต์
# ๋ช ๋ฒ์ ์ ์ ํ
์ค๋ฅผ ์ฌ์ฉํ ์ง ์ค์ ํด ์ค๋๋ค.
swagger: "2.0"
info:
version: "0.0.1"
title: tistory test
# swagger๊ฐ ์คํ๋๊ณ ์๋ host๋ฅผ ์ค์ ํด ์ค๋๋ค.
host: localhost:4000
# url์์ ์ฌ์ฉํ basePath๋ฅผ ์ค์ ํด ์ค๋๋ค.
basePath: /api/v1.0
schemes:
# ํ๋กํ ์ฝ์ ์ค์ ํด ์ฃผ๋ ๊ณณ์ธ๋ฐ, Production์ผ๋ก ์ฌ์ฉ์ http๋ฅผ ์ ๊ฑฐํ๋ ๊ฒ์ ๊ถ์ฅํฉ๋๋ค.
- http
- https
# ์๋ฒ๋ก ๋ณด๋ผ ๋ฐ์ดํฐ์ Content-type์ ์ค์ ํด ์ค๋๋ค.
consumes:
- application/json
# ํด๋ผ์ด์ธํธ์๊ฒ ์ ์กํ ๋ฐ์ดํฐ์ ํ์
์ ์ค์ ํด ์ค๋๋ค.
produces:
- application/json
API ์์ฑ ํผ์ณ๋ณด๊ธฐ
paths:
/user/{user_name}:
post:
tags:
- "tag-for-user"
summary: "api-summary"
description: "swagger-example"
consumes:
- "application/x-www-form-urlencoded"
# parameters์ ๋ํ ๋ด์ฉ์ ๋ช
์ธํ ๋ถ๋ถ ์
๋๋ค.
# ์ด ๋ถ๋ถ์์ ์์ฑํ๋ ์์๋ ํฐ ์๋ฏธ๊ฐ ์๋๊ฒ ๊ฐ์ต๋๋ค.
# in: path, header, body, query ์ ๋ ๋ณดํต ๋ค๊ฐ์ง ํํ๋ฅผ ์ฃผ๋ก ์ฌ์ฉํฉ๋๋ค.
# name : ๋ณ์๋ช
์ด๋ผ๊ณ ์๊ฐํ์๋ฉด ๋ฉ๋๋ค. ์์ ๋ค๊ฐ์ง ๋ฐฉ์์ ํตํด์ ๋์ด์ค๋ ๋ณ์์ ์ด๋ฆ์
๋๋ค.
# type : ๋ฐ์ดํฐ ํ์
์ ๋ช
์ํด ์ค๋๋ค. string, boolean, integer, number, array ... ๋ฑ ์ฌ๋ฌ๊ฐ์ง ๋ฐ์ดํฐ ํ์
์ด ์์ต๋๋ค.
# scheme $ref: ๋ฐ์ ๋ถ๋ถ์ model ์์ฑ์ ํตํด ์ฐธ์กฐํ์ฌ ์ฌ์ฉ ๊ฐ๋ฅํฉ๋๋ค.
parameters:
- name: "user_name"
in: "path"
description: "spec for user_name"
required: true
type: "string"
- name: "queryString-exam"
in: "query"
description: "offset for pagination"
required: false
type: "integer"
- name: "header-exam"
in: "header"
required: true
type: "string"
- in: "body"
name: "user"
required: false
schema:
$ref: "#/definitions/user"
# ์์ฒญ์ด ๋ค์ด์์๋ ์๋ต์ ๋ํ ๋ช
์ธ๋ฅผ ํํํฉ๋๋ค.
# ๊ฐ ์๋ต ์ฝ๋๋ณ ์๋ต ๋ฉ์์ง์ ํฌ๋ฉง์ ์ค์ ํด ์ค ์ ์์ต๋๋ค.
responses:
200:
description: "status code 200"
schema:
$ref: "#/definitions/user"
401:
description: "Authentication Error"
schema:
$ref: "#/definitions/SuccessResponse"
500:
description: "status code 500"
schema:
$ref: "#/definitions/ErrorResponse"
# Response์ Request์ ์ฌ์ฉํ Model์ ์ ์ํด์ฃผ๋ ๋ถ๋ถ ์
๋๋ค.
# Example ๋ฐ์ดํฐ๋ ์ฝ์
์ด ๊ฐ๋ฅํ๋ฉฐ Swagger-Inspecter๋ฅผ ํตํ ํ
์คํธ ๋ฐ์ดํฐ๋ก ์ฌ์ฉ๋ฉ๋๋ค.
definitions:
user:
type: "object"
required:
- "username"
properties:
id:
type: "integer"
description: "The user ID."
username:
type: "string"
description: "The user name."
example:
id: 0
username: "username"
SuccessResponse:
properties:
code:
type: integer
status:
type: string
message:
type: string
ErrorResponse:
properties:
code:
type: integer
status:
type: string
message:
type: string
swagger-codegen generate -i {ํ์ผ ์ด๋ฆ} -l {์ธ์ด}

- swagger-codegen ๋ช
๋ น์ด๋ฅผ ํตํด server stub์ ์์ฑํ ์ ์์
- ๊ธฐ๋ณธ์ ์ผ๋ก MVC ํจํด์ ๋ง์ถฐ์ ํ๋กํ ํ์
์ด ์์ฑ๋จ

- npm install์ ํตํด Dependency๋ฅผ ๋ชจ๋ ๋ง์ถฐ์ฃผ๊ณ ํ์ผ์ ์คํ
- ๊ธฐ๋ณธ์ ์ผ๋ก Swagger-codegen์ ์คํํ๋ ๊ณผ์ ์ ํตํด Swagger-UI๋ ๊ฐ์ด ์ค์น๋๋๊ฒ์ ํ์ธ
- ๊ธฐ์กด์ ํ๋ก์ ํธ์ Swgger-Editor ํน์ Swagger-UI๋ฅผ ์ค์นํ๋ ๊ฒฝ์ฐ npm์ ์๋ ํจํค์ง๋ฅผ ์ฌ์ฉํ์ฌ ์ค์นํ ์ ์์

- ๋ช
์๋์ด ์๋๊ฒ ์ฒ๋ผ ํด๋น์ฃผ์(localhost:3000/docs)๋ก ์ ๊ทผ์ ํ๊ฒ๋๋ฉด Swagger-ui ํ์ด์ง๋ก ์ด๋ํ ์ ์์
- ๋
๋ฆฝ์ ์ธ UI๋ฅผ ๊ฐ์ง ํ์ด์ง๋ฅผ ์คํ์ํฌ ์ ์์
- Spec์ ๋ํด ํ์ธ์ด ๊ฐ๋ฅํ๊ณ ์ ์ํ API์ ๋ํด TEST๋ ์งํํ ์ ์์
- ์ต์ ๋ฒ์ ์ธ Swagger 3.x ๋ฒ์ ์ ์ ์ฉ
dependencies {
implementation 'io.springfox:springfox-boot-starter:3.0.0'
}
@Configuration
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.OAS_30)
.useDefaultResponseMessages(false)
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.springswagger.controller"))
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Practice Swagger")
.description("practice swagger config")
.version("1.0")
.build();
}
}
- Docket: Swagger ์ค์ ์ ํต์ฌ์ด ๋๋ Bean
- useDefaultResponseMessages: Swagger ์์ ์ ๊ณตํด์ฃผ๋ ๊ธฐ๋ณธ ์๋ต ์ฝ๋ (200, 401, 403, 404). false ๋ก ์ค์ ํ๋ฉด ๊ธฐ๋ณธ ์๋ต ์ฝ๋๋ฅผ ๋
ธ์ถํ์ง ์์
- apis: api ์คํ์ด ์์ฑ๋์ด ์๋ ํจํค์ง (Controller) ๋ฅผ ์ง์
- paths: apis ์ ์๋ API ์ค ํน์ path ๋ฅผ ์ ํ
- apiInfo:Swagger UI ๋ก ๋
ธ์ถํ ์ ๋ณด
5-3. Controller ์ ์ ์ฉ
@RestController
public class HelloController {
@Operation(summary = "test hello", description = "hello api example")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "OK !!"),
@ApiResponse(responseCode = "400", description = "BAD REQUEST !!"),
@ApiResponse(responseCode = "404", description = "NOT FOUND !!"),
@ApiResponse(responseCode = "500", description = "INTERNAL SERVER ERROR !!")
})
@GetMapping("/hello")
public ResponseEntity<String> hello(@Parameter(description = "์ด๋ฆ", required = true, example = "Park") @RequestParam String name) {
return ResponseEntity.ok("hello " + name);
}
}
6. ์ ์ฉ๋ ์ค์ ํ์ธ
- SwaggerConfig ์์ ์ค์ ํ ์ ๋ณด๋ค์ Swagger ์๋จ์ ๋์ด
- Controller ์์ ์ธํ
ํ ์ค์
- Swagger ๋ SpringFox ์ธ์ SpringDoc ์ผ๋ก๋ ์ค์ ํ ์ ์์
- ํ์ฌ Spring Boot 2.6 ๋ฒ์ ์์ SpringFox Swagger 3.0 ์ ์ฌ์ฉํ์ง ๋ชปํ๋ ์ด์๊ฐ ์กด์ฌ > SpringDoc ๊ณต์ ๋ฌธ์ ์ฐธ๊ณ , Spring Boot๋ฅผ 2.5 ๋ฒ์ ๋๋ก ๋ด๋ฆฌ์ง ์์๋ Swagger๋ฅผ ์ ์ฉํ ์ ์์