[Spring] - SpringBoot 3.x ๋ฒ„์ „์—์„œ Swagger ์‚ฌ์šฉํ•˜๊ธฐ

๐Ÿ› ๏ธ ๊ฐœ๋ฐœ ํ™˜๊ฒฝ

๐Ÿƒ Spring : Spring Boot 3.1.3

๐Ÿ› ๏ธ Java : Amazon corretto 17

๐Ÿ“” ์˜์กด์„ฑ

Spring Boot 3.x ๋ฒ„์ „๋ถ€ํ„ฐ springfox ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ง€์›ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— springdoc-openapi๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.2'

๐Ÿ› ๏ธ ์ ์šฉ

springfox ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ์—ฌ๋Ÿฌ ์„ค์ •์„ ํ•ด์ค˜์•ผํ–ˆ์ง€๋งŒ, springdoc-openapi์€ ์„ค์ • ์กฐ์ฐจ ํ•„์š”์—†์ด ๋ฐ”๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

public class TestController {

    @Operation(
            summary = "ํ…Œ์ŠคํŠธ ์š”์ฒญ",
            description = "ํ…Œ์ŠคํŠธ ์š”์ฒญ์— ๋Œ€ํ•œ API"
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "ํ…Œ์ŠคํŠธ ์„ฑ๊ณต"),
            @ApiResponse(responseCode = "204", description = "์กฐํšŒ ๋ฐ์ดํ„ฐ ์—†์Œ"),
            @ApiResponse(responseCode = "400", description = "์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ ์˜ค๋ฅ˜")
    })
    @GetMapping("")
    public ResponseData.ApiResult<Area> testApi() {

        ...

        return ResponseData.success(area);
    }
}

์œ„์™€ ๊ฐ™์ด @Operation๋ฅผ ์ด์šฉํ•ด API์— ๋Œ€ํ•œ ์„ค๋ช…์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๊ณ , @ApiResponse๋ฅผ ํ†ตํ•ด ๋ฐ˜ํ™˜ ๋ฐ์ดํ„ฐ๋‚˜ ์ฝ”๋“œ๋ฅผ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿงฉ ํ…Œ์ŠคํŠธ

์„œ๋ฒ„๋ฅผ ์‹คํ–‰ํ•˜๊ณ , ์•„๋ž˜ URL์„ ํ†ตํ•ด ์ ‘์†ํ•˜๋ฉด API ๋ช…์„ธ๋ฅผ ์ž์„ธํ•˜๊ฒŒ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

http://localhost:8080/swagger-ui/index.html

Reference

resilient๋‹˜ ๋ธ”๋กœ๊ทธ