Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
๐ Swagger ์ ์ฉ ๋ฌธ์ ์ ๋ฆฌ(#16)
- Loading branch information
Showing
1 changed file
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
# Swagger ์ ์ฉ | ||
|
||
> ์ ์ฒด ์ฝ๋ ์ ์ฉ ๊ณผ์ ์ [github issue](https://github.com/jun108059/chat-server/issues/16) ์์ ํ์ธํ ์ ์์ต๋๋ค. | ||
- springdoc ๊ณผ springfox ๋น๊ต | ||
- [springdoc ๊ณต์๋ฌธ์](https://springdoc.org/)๋ฅผ ์ฐธ๊ณ ํด์ ์์ฑ | ||
- ์์ฑ์ผ(22.07.30) ๊ธฐ์ค ์ต์ ๋ฒ์ : springdoc-openapi v1.6.9 | ||
|
||
## 1. springdoc vs springfox | ||
|
||
> ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ชจ๋ Spring Framework๋ฅผ ์ฌ์ฉํ๋ ํ๋ก์ ํธ์์ Swagger๋ฅผ ์ด์ฉํด API ๋ฌธ์๋ฅผ ์ฝ๊ฒ ์ธ ์ ์๋๋ก ํด์ฃผ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ | ||
- 2012 ~ 2015 : `Swagger SpringMVC` ์ด๋ฆ์ผ๋ก springfox ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ๋ง์ด ์ฌ์ฉ ๋จ | ||
- 2015 ~ 2018 : `springfox-swagger` ์ ๋ฐ์ดํธ๊ฐ ํ๋ฐํ ์ด๋ฃจ์ด์ง | ||
- 2018 ~ 2020 : fox ์ ๋ฐ์ดํธ๊ฐ ์งํ๋์ง ์์ `springdoc` ๋ฑ์ฅ, ๊ฐ์ธ | ||
- 2020 ~ ํ์ฌ : `springfox`๋ ๋ค์ ์ ๋ฐ์ดํธ ์์ํ์ฌ `springdoc` ๋น์ทํ๊ฒ ํ์ฉ ์ค | ||
|
||
<img width="1166" alt="image" src="https://user-images.githubusercontent.com/42997924/181807257-846fbe25-4d5e-4a2a-b4fb-683fc9068d09.png"> | ||
|
||
(์ถ์ฒ : [Google Trends](https://trends.google.com/trends/explore?date=2019-06-10%202022-07-30&q=springfox,springdoc)) | ||
|
||
### 1-1. springdoc ์ ํ | ||
|
||
ํ๋ก์ ํธ์๋ `springdoc`์ ์ ํํ๋ค. ๊ณต์๋ฌธ์๊ฐ ์ฝ๊ธฐ ํธํ๊ณ , API ๊ทธ๋ฃนํ ์ค์ ์ด ๊ฐ๋จํ๋ค. | ||
|
||
์์ฑ์ผ ๊ธฐ์ค ๋ ํ๋ฐํ ์ ๋ฐ์ดํธ ๋๊ณ ์๊ธฐ๋ ํ๋ค. | ||
|
||
## Swagger OpenAPI 3.0 | ||
|
||
- ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ OpenAPI 3.0 ์คํ์ ๋ง๋ JSON์ ์๋์ผ๋ก ๋ง๋ค์ด ์ฃผ๋ฉด, Swagger UI๋ ๋ง๋ค์ด์ง JSON์ ํ๋ฉด์ ํ์ ํด์ค๋ค. | ||
|
||
![image](https://user-images.githubusercontent.com/42997924/181805315-d8f1abd3-c661-4e78-8ec3-f953f095b563.png) | ||
|
||
(์ถ์ฒ : [springdoc ๊ณต์๋ฌธ์](https://springdoc.org/)) | ||
|
||
`springdoc-openapi-ui`๋ฅผ ์ดํด๋ณด๋ฉด swagger-ui๋ฅผ ํฌํจํ๊ณ ์๋ ๊ฒ์ ํ์ธํ ์ ์๋ค. | ||
|
||
- `swagger-ui` : ํต์ฌ ๋ก์ง ๊ตฌํ | ||
- `springdoc-openapi` : swagger-ui๋ฅผ ์ถ์ํํด์ ๋ ์ ํ์ฉํ ์ ์๊ฒ ํด์ฃผ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ | ||
|
||
## 2. ํ๋ก์ ํธ์ ์ ์ฉ | ||
|
||
Gradle, yml ๊ธฐ๋ฐ์ผ๋ก ์ ์ฉํ๋ค. | ||
|
||
### 2-1. dependency ์ถ๊ฐ | ||
|
||
```groovy | ||
// build.gradle ํ์ผ | ||
dependencies { | ||
implementation 'org.springdoc:springdoc-openapi-ui:1.6.9' | ||
} | ||
``` | ||
|
||
### 2-2. yml ์ค์ ์ถ๊ฐ | ||
|
||
```yaml | ||
springdoc: | ||
swagger-ui: | ||
path: /swagger-ui.html | ||
groups-order: DESC | ||
operationsSorter: method | ||
disable-swagger-default-url: true | ||
display-request-duration: true | ||
api-docs: | ||
path: /api-docs | ||
show-actuator: true | ||
default-consumes-media-type: application/json | ||
default-produces-media-type: application/json | ||
paths-to-match: | ||
- /v1/** | ||
``` | ||
|
||
### 2-3. SwaggerConfig ์ค์ ๋ฑ๋ก | ||
|
||
```java | ||
// SwaggerConfig.java | ||
@OpenAPIDefinition( | ||
info = @Info(title = "์ฑํ ์๋น์ค API ๋ช ์ธ์", | ||
description = "ํฅ์ฌ๊ณ ๋ ์ํคํ ์ฒ ๊ธฐ๋ฐ ์ฑํ ์๋น์ค API ๋ช ์ธ์", | ||
version = "v1")) | ||
@RequiredArgsConstructor | ||
@Configuration | ||
public class SwaggerConfig { | ||
|
||
@Bean | ||
public GroupedOpenApi chatOpenApi() { | ||
String[] paths = {"/v1/**"}; | ||
|
||
return GroupedOpenApi.builder() | ||
.group("์ฑํ ์๋น์ค API v1") | ||
.pathsToMatch(paths) | ||
.build(); | ||
} | ||
} | ||
``` | ||
|
||
> ์ดํ์ JWT ํ ํฐ ๊ด๋ จ ์ ๋ณด๋ ์ฌ๊ธฐ์ ์ถ๊ฐํด์ฃผ๋ฉด ์ข๋ค. | ||
### 2-4. ์ปจํธ๋กค๋ฌ์ ์ ์ฉ | ||
|
||
```java | ||
// DeleteMemberController.java | ||
@RestController | ||
@RequiredArgsConstructor | ||
public class DeleteMemberController { | ||
|
||
@Operation(summary = "ํ์ ํํด ์์ฒญ", description = "ํ์ ์ ๋ณด๊ฐ ์ญ์ ๋ฉ๋๋ค.", tags = { "Member Controller" }) | ||
@ApiResponses({ | ||
@ApiResponse(responseCode = "200", description = "OK", | ||
content = @Content(schema = @Schema(implementation = DeleteMemberResponse.class))), | ||
@ApiResponse(responseCode = "400", description = "BAD REQUEST"), | ||
@ApiResponse(responseCode = "404", description = "NOT FOUND"), | ||
@ApiResponse(responseCode = "500", description = "INTERNAL SERVER ERROR") | ||
}) | ||
@DeleteMapping("/v1/member/{id}") | ||
ResponseEntity<DeleteMemberResponse> deleteMember( | ||
@Parameter(description = "ํ์ ID", required = true, example = "1") @PathVariable("id") Long id) { | ||
// ์๋ต.. | ||
} | ||
} | ||
``` | ||
|
||
## 3. ๋์์ ํ์ธ | ||
|
||
์ ๊ทผ URL : http://localhost:8080/swagger-ui.html | ||
|
||
<img width="1297" alt="image" src="https://user-images.githubusercontent.com/42997924/181805001-585b27dd-6c56-43db-a70c-1c0d3fc591e6.png"> | ||
|
||
## 4. ์ ๋ฆฌ | ||
|
||
Code Base ์ Swagger ๊ด๋ จ ์ฝ๋๊ฐ ๋ง์ด ๋ค์ด๊ฐ๊ฒ ๋๋ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด TDD์ ๋ ์ต์ํด์ง๋ฉด RestDoc์ผ๋ก ๋ณ๊ฒฝํด๋ณด๋ ค๊ณ ํ๋ค. | ||
|
||
์ต๊ทผ์ ์คํฐ๋ํ๋ฉด์ ๊ด๋ จ ๋ด์ฉ์ ๋ ผ์ํ ์ ์ด ์๋๋ฐ, ์ด๋ค ๋ถ์ด ์ปจํธ๋กค๋ฌ ์ธํฐํ์ด์ค๋ฅผ ๋ง๋ค์ด์ Swagger ๊ด๋ จ ๋ด์ฉ์ ์ธํฐํ์ด์ค์ ์์ฑํ ๊ฒ์ ๋ดค๋๋ฐ ๊ทธ๊ฒ๋ ์ข์ ๋ฐฉ๋ฒ์ธ ๊ฒ ๊ฐ๋ค! | ||
|
||
๋ฆฌํํฐ๋งํ ๋ ํ๋ฒ ์ ์ฉํด๋ณด๋ฉด ์ข์ ๊ฒ ๊ฐ๋ค. | ||
|
||
**Reference** | ||
|
||
- [springdoc ๊ณต์๋ฌธ์](https://springdoc.org/) | ||
- [springfox-swagger ๊ณต์๋ฌธ์](http://springfox.github.io/springfox/docs/current/) |