Swagger with WebFlux - ryoobib/backend_webflux GitHub Wiki
WebFlux ํ๋ก์ ํธ์ Swagger ์ ์ฉํ๊ธฐ
์ฌ์ฉ ์ด์
Swagger๋ฅผ ์์ํ๊ฒ ์ฌ์ฉํด๋ณด๊ณ ์ถ์ด์ ์ ์ฉํ๋ค. ์ด์ ํ๋ก์ ํธ์์๋ Rest Docs๋ฅผ ์ฌ์ฉํ๋๋ฐ, ๋ํ์ ์ธ ํ ์คํธ ๋ฌธ์ํ ๋๊ตฌ์ธ ๋ ๊ฐ๋ฅผ ๋ชจ๋ ์ฌ์ฉํด๋ณด๊ณ ๋น๊ตํด๋ณด๊ณ ์ถ์๋ค.
1. ์์กด์ฑ ์ถ๊ฐํ๊ธฐ
plugins {
id 'org.springframework.boot' version '2.7.1'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id "org.springdoc.openapi-gradle-plugin" version "1.3.4" // ์ถ๊ฐ
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb-reactive'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-webflux'
implementation 'org.springdoc:springdoc-openapi-webflux-ui:1.6.9' // ์ถ๊ฐ
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.projectreactor:reactor-test'
annotationProcessor "org.springframework.boot:spring-boot-configuration-processor"
}
tasks.named('test') {
useJUnitPlatform()
}
application.yml
ํ์ผ ์์ ํ๊ธฐ
2. server:
servlet:
context-path: /
compression:
enabled: true
mime-types: text/html,text/xml,text/plain,text/css,text/javascript,application/javascript,application/json
min-response-size: 1024
http2:
enabled: true
com:
example:
backend_webflux:
springdoc:
api:
router:
separete:
enabled: false
common:
enabled: true
Router-Handler ๋ฐฉ์์ผ๋ก endpoint ์ธ์ํ๋๋ก ํ๊ธฐ ์ํ ์ค์ ์ด๋ค.
3. Router์ Swagger ์ด๋ ธํ ์ด์ ์์ฑํ๊ธฐ
@Configuration
public class UserRouter {
@Bean
@GetAllUserApiInfo
public RouterFunction<ServerResponse> getAllUserRouter(UserHandler userHandler) {
return RouterFunctions
.route(GET("/api/user")
, userHandler::getAllUser)
;
}
@Bean
@GetUserByIdApiInfo
public RouterFunction<ServerResponse> getUserRouter(UserHandler userHandler) {
return RouterFunctions
.route(GET("/api/user/{id}")
, userHandler::getUser);
}
@Bean
@CreateUserApiInfo
public RouterFunction<ServerResponse> createUserRouter(UserHandler userHandler) {
return RouterFunctions
.route(POST("api/user")
, userHandler::createUser);
}
@Bean
@ModifyUserByIdApiInfo
public RouterFunction<ServerResponse> modifyUserRouter(UserHandler userHandler) {
return RouterFunctions
.route(PUT("api/user/{id}")
, userHandler::modifyUser);
}
@Bean
@DeleteUserByIdApiInfo
public RouterFunction<ServerResponse> deleteUserRouter(UserHandler userHandler) {
return RouterFunctions
.route(DELETE("api/user/{id}")
, userHandler::deleteUser);
}
}
๊ธฐ์กด์ Chaining ๋ฐฉ์์์ Separate ๋ฐฉ์์ผ๋ก ๋ณ๊ฒฝํ์๋ค. ๊ฐ๋ณ Router ๋ง๋ค Swagger API Info๋ฅผ ์ด๋ ธํ ์ด์ ์ผ๋ก ์ ์ฉํ๊ธฐ ์ํจ์ด๋ค.
4. Swagger API Info ์ด๋ ธํ ์ด์ ์์ฑํ๊ธฐ
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
@RouterOperations({
@RouterOperation(
method = RequestMethod.GET,
operation =
@Operation(
description = "Get all users ",
operationId = "getAllUser",
tags = "users",
responses = {
@ApiResponse(
responseCode = "200",
description = "Get all users endpoint",
content = {
@Content(
mediaType = MediaType.APPLICATION_JSON_VALUE,
array = @ArraySchema(schema = @Schema(implementation = User.class)))
}),
@ApiResponse(
responseCode = "400",
description = "Not found response",
content = {
@Content(
mediaType = MediaType.APPLICATION_JSON_VALUE,
schema = @Schema(implementation = ExceptionResponse.class))
})
}))
})
public @interface GetAllUserApiInfo {
}
Swagger-ui์์ ๋ณด๊ณ ์ถ์ ์ ๋ณด๋ฅผ ์์ฑํ๋ฉด ๋๋ค.
์๋ ๋ธ๋ก๊ทธ๋ฅผ ์ฐธ๊ณ ํ์๋ค.
Swagger ์ ์ฉ ํ ๋๋ ์
Swagger๋ฅผ ์ด์ฉํ๋ฉด Router ํด๋์ค์ ์ง์ ์ ์ธ ์ฝ๋ ์์ฑ์ผ๋ก ๊ฐ๋ ์ฑ์ ํด์น๋ค๋ ์ ์ ์ฌ๋ฌ ๋ธ๋ก๊ทธ๋ฅผ ํตํด ๋ณธ ์ ์ด ์๋ค. ์ด๋ ์ด๋ ธํ ์ด์ ์ ์ด์ฉํด ๋ฐ๋ก ๋ถ๋ฆฌ๋ก ๊ฐ๋ ์ฑ์ ํ๋ณดํ ์ ์๋ค๋ ์ ์์ ํฌ๊ฒ ๊ณต๊ฐํ์ง ๋ชปํ์๋ค. Rest Docs์ ๋ค๋ฅด๊ฒ ํ ์คํธ ์ฝ๋๋ฅผ ์์ฑํ์ง ์์๋ ๋๋ค๋ ์ ์์ ์ผ๊ฐ์ด ์ค์๋ค๊ณ ๋๋ ์๋ ์์ผ๋ ์ด๋ ธํ ์ด์ ์ ์ปค์คํฐ๋ง์ด์ง ํ๋ฉด์ ์์ฑํ๋ ์ฝ๋์ ์๋ ๋ง๋ง์น ์๊ธฐ ๋๋ฌธ์ ํฌ๊ฒ ๋ ๋๊ตฌ ๋ชจ๋ ์ฝ๋ ์์ฑ์ ์์ผ๋ก ๋น๊ตํ๋ ๊ฒ์ ๋ฐ๋์งํ์ง ์์ ๊ฑฐ ๊ฐ๋ค.
ํผ์ ๊ฐ๋ฐ์ ํ๋ค ๋ณด๋, ๋ค๋ฅธ ์ฌ๋๊ณผ ๋ฌธ์ ๊ณต์ ๋ฅผ ํ ์ผ์ด ์์ด์ swagger์ ์ฅ์ ์ ์ต๋๋ก ๋๊ปด ๋ณด์ง๋ ๋ชปํ๋ค. ๊ทธ๋ฌ๋ ์ ์ฉ์ด ์ฝ๊ณ , ์ค์ ์ด ๋ง์ง ์์ ์ ์์๋ ๋งค๋ ฅ์ ์ด์๋ค.