๐ฑ Swagger๋?
- REST API ๋ฅผ ์ค๊ณ, ๋น๋, ๋ฌธ์ํ ๋ฐ ์ฌ์ฉํ๋๋ฐ ๋์์ด ๋๋ OpenAPI ์ฌ์์ ์ค์ฌ์ผ๋ก ๊ตฌ์ถ๋ ์คํ ์์ค ๋๊ตฌ ์ธํธ
- ์ฝ๋ ๋ช ์ค ์ถ๊ฐ๋ฅผ ํตํด ์ ์ฉํ๊ธฐ ์ฌ์ฐ๋ฉฐ, ๋ฌธ์ ํ๋ฉด์์ UI๋ฅผ ํตํด ๋ฐ๋ก API ํ ์คํธ๊ฐ ๊ฐ๋ฅํ ์ฅ์
๐ฑ Swagger ์ฌ์ฉ ๋ฐฉ๋ฒ
- build.gradle ์ ์์กด์ฑ ์ถ๊ฐ : 2.9.2 ๋ฒ์
// build.gradle
dependencies {
compile('io.springfox:springfox-swagger2:2.9.2')
compile('io.springfox:springfox-swagger-ui:2.9.2')
}
- Swagger ์ค์ ์ถ๊ฐ
// config/SwaggerConfig.java
@Configuration
@EnableSwagger2
public class SwaggerConfig { // Swagger
private static final String API_NAME = "ToyProject API";
private static final String API_VERSION = "0.0.1";
private static final String API_DESCRIPTION = "ToyProject API ๋ช
์ธ์";
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.toyproject.book.springboot")) // Swagger๋ฅผ ์ ์ฉํ ํด๋์ค์ package๋ช
.paths(PathSelectors.any()) // ํด๋น package ํ์์ ์๋ ๋ชจ๋ url์ ์ ์ฉ
.build()
.apiInfo(apiInfo());
}
public ApiInfo apiInfo() { // API์ ์ด๋ฆ, ํ์ฌ ๋ฒ์ , API์ ๋ํ ์ ๋ณด
return new ApiInfoBuilder()
.title(API_NAME)
.version(API_VERSION)
.description(API_DESCRIPTION)
.build();
}
}
- Controller์ Swagger ์ด๋ ธํ ์ด์ ์ถ๊ฐ : Api, ApiOperation, ApiImplicitParam
// PostsApiController.java
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/v1")
@Api(tags = {"ToyProject API Test"}) // Swagger ์ต์๋จ Controller ๋ช
์นญ
public class PostsApiController {
private final PostsService postsService;
@GetMapping("/home") // ์คํ๋ง๋ถํธ ๋ฆฌ์กํธ ์ฐ๋ ํ
์คํธ
@ApiOperation(value = "์ฐ๋ ํ
์คํธ", notes = "์คํ๋ง๋ถํธ์ ๋ฆฌ์กํธ ์ฐ๋์ ํ
์คํธํ๋ค.") // Swagger์ ์ฌ์ฉํ๋ API์ ๋ํ ๊ฐ๋จ ์ค๋ช
public String getHome() {
return "Hello World!";
}
@PostMapping("/posts") // ๋ฑ๋ก API
@ApiOperation(value = "๊ธ ๋ฑ๋ก", notes = "๊ธ ๋ฑ๋ก API")
public Long save(@RequestBody PostsSaveRequestDto requestDto) {
return postsService.save(requestDto);
}
@GetMapping("/posts/{id}") // ์กฐํ API
@ApiOperation(value = "๊ธ ์กฐํ", notes = "๊ธ ์กฐํ API")
@ApiImplicitParam(name = "id", value = "๊ธ ์์ด๋") // Swagger์ ์ฌ์ฉํ๋ ํ๋ผ๋ฏธํฐ์ ๋ํด ์ค๋ช
public PostsResponseDto findById (@PathVariable Long id) {
return postsService.findById(id);
}
@PutMapping("/posts/{id}") // ์์ API
@ApiOperation(value = "๊ธ ์์ ", notes = "๊ธ ์์ API")
@ApiImplicitParam(name = "id", value = "๊ธ ์์ด๋")
public Long update(@PathVariable Long id, @RequestBody PostsUpdateRequestDto requestDto){
return postsService.update(id, requestDto);
}
@DeleteMapping("/posts/{id}") // ์ญ์ API
@ApiOperation(value = "๊ธ ์ญ์ ", notes = "๊ธ ์ญ์ API")
@ApiImplicitParam(name = "id", value = "๊ธ ์์ด๋")
public Long delete(@PathVariable Long id){
postsService.delete(id);
return id;
}
}
- ๋ฐ์ดํฐ๋ฅผ ๋ฐ์ RequestBody ํ์์ ํ๋ผ๋ฏธํฐ ํํ : ApiModelProperty
// PostsResponseDto
@Getter
public class PostsResponseDto {
@ApiModelProperty(example = "๊ธ ์์ด๋") // Swagger์ ํด๋น ํ๋๊ฐ ๋ฌด์์ธ์ง ๋ํ๋
private Long id;
@ApiModelProperty(example = "๊ธ ์ ๋ชฉ")
private String title;
@ApiModelProperty(example = "๊ธ ์ค๋ช
")
private String description;
@ApiModelProperty(example = "๊ณต๊ตฌ ๊ด๋ จ ๋งํฌ")
private String link;
@ApiModelProperty(example = "๊ณต๊ตฌ ์คํ์ฑํ
๋งํฌ")
private String contact;
@ApiModelProperty(example = "๊ณต๊ตฌ ๊ฐ๊ฒฉ")
private String price;
@ApiModelProperty(example = "๊ณต๊ตฌ ๋ ์ง")
private String date;
@ApiModelProperty(example = "์์ฑ์")
private String author;
public PostsResponseDto(Posts entity) {
this.id = entity.getId();
this.title = entity.getTitle();
this.description = entity.getDescription();
this.link = entity.getLink();
this.contact = entity.getContact();
this.date = entity.getDate();
this.price = entity.getPrice();
this.author = entity.getAuthor();
}
}
๐ฑ Swagger ์ฌ์ฉํด๋ณด๊ธฐ
- ์๋ฒ ์คํ ํ localhost:8080/swagger-ui.html
- Try it out ๋ฒํผ ํด๋ฆญ ํ ํ๋ผ๋ฏธํฐ๊ฐ ํ์ํ ๊ฒฝ์ฐ ์์ฑ ํ Execute
- ์ฐ๋ ํ ์คํธ : /api/v1/home
- ๊ธ ๋ฑ๋ก : /api/v1/posts
- ๊ธ ์กฐํ : /api/v1/posts/{id}
- ๊ธ ์์ : /api/v1/posts/{id}
- ๊ธ ์ญ์ : /api/v1/posts/{id}
์ฐธ๊ณ ์๋ฃ
'Java-Spring > etc' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Spring Boot] Spring Redis (0) | 2023.06.21 |
---|---|
[Spring Boot] Spring Batch (0) | 2023.06.18 |
[Spring Boot] Jsoup ์น ํฌ๋กค๋ง๊ณผ DB ์ ์ฅ (0) | 2022.03.31 |