๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ฐœ๋ฐœ/Spring

Spring boot with Swagger 3.0

by 1mj 2022. 11. 3.

OpenAPI๋ž€?

OpenAPI Specification(OAS)๋ผ๊ณ ๋„ ํ•˜๋Š” OpenAPI๋Š” RESTful API๋ฅผ ๊ธฐ์ •์˜๋œ ๊ทœ์น™์— ๋งž๊ฒŒ API Spec์„ json์ด๋‚˜ yaml๋กœ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฉ์‹์„ ์˜๋ฏธํ•œ๋‹ค.

  • RESTful API ๋ฅผ ๊ธฐ์ˆ ํ•˜๋Š” ํ‘œ์ค€์œผ๋กœ ์„œ๋น„์Šค์—์„œ ์ œ๊ณตํ•˜๋Š” API ์˜ ๊ธฐ๋Šฅ๊ณผ End Point ๋ฅผ ๊ฐœ๋ฐœ์ž๋‚˜ ์‹œ์Šคํ…œ์ด ์ž๋™์œผ๋กœ ๋ฐœ๊ฒฌํ•˜๊ณ  ์ฒ˜๋ฆฌํ•˜๋Š”๋ฐ ํ•„์š”ํ•œ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•œ๋‹ค.
  • ์ „์—๋Š” Swagger 2.0๊ณผ ๊ฐ™์€ ์ด๋ฆ„์œผ๋กœ ๋ถˆ๋ ธ๋‹ค๊ฐ€ 3.0๋ฒ„์ „์œผ๋กœ ์˜ฌ๋ผ์˜ค๋ฉด์„œ OpenAPI 3.0 Specification(๋˜๋Š” Swagger 3.0)์œผ๋กœ ์นญํ•œ๋‹ค.
๐Ÿ”Ž Open API(Open Application Programming Interface, ๊ณต๊ฐœ API)
๋ˆ„๊ตฌ๋‚˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ณต๊ฐœ๋œ API๋ฅผ ๋งํ•œ๋‹ค. Public API๋ผ๊ณ ๋„ ๋ถ€๋ฅธ๋‹ค.

 

Swagger๋ž€?

๊ฐ„๋‹จํ•œ ์„ค์ •์œผ๋กœ API ๋ฌธ์„œ๋ฅผ ์ž๋™ํ™”ํ•ด์ฃผ๋Š” ์˜คํ”ˆ์†Œ์Šค ํ”„๋ ˆ์ž„์›Œํฌ์ด๋‹ค.

  • OpenAPI ์‚ฌ์–‘์„ ์ค‘์‹ฌ์œผ๋กœ ๊ตฌ์ถ•๋œ ์˜คํ”ˆ์†Œ์Šค ๋ฌธ์„œํ™” ๋„๊ตฌ์ด๋‹ค.
  • API ๋ช…์„ธ์„œ๋ฅผ ์ง์ ‘ ์ž‘์„ฑํ•˜๋Š” ๋Œ€์‹  Swagger๋ฅผ ์‚ฌ์šฉํ•ด API๋ฅผ ๋ฌธ์„œํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • Spring REST Docs์™€ ๋‹ฌ๋ฆฌ ์ฝ”๋“œ ๋ช‡ ์ค„๋งŒ ์ถ”๊ฐ€ํ•˜์—ฌ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.
  • ๋ฌธ์„œ ํ™”๋ฉด์—์„œ API๋ฅผ ๋ฐ”๋กœ ํ…Œ์ŠคํŠธํ•  ์ˆ˜ ์žˆ๋‹ค.
  • API ๋ฌธ์„œ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ์‹œ๊ฐ„์„ ์ ˆ์•ฝํ•˜๊ณ  API ์ •๋ณด๋ฅผ ์‹ค์‹œ๊ฐ„์œผ๋กœ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

Spring boot์— Swagger 3.0 ์ ์šฉํ•˜๊ธฐ

1. Swagger ์‚ฌ์šฉ์„ ์œ„ํ•œ ์˜์กด์„ฑ ์ถ”๊ฐ€(springfox ์‚ฌ์šฉ)

implementation "io.springfox:springfox-boot-starter:3.0.0"
implementation "io.springfox:springfox-swagger-ui:3.0.0"

 

2. Swagger ์„ค์ •

@Configuration
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.OAS_30)
                .useDefaultResponseMessages(false)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.sample.controller"))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Practice Swagger")
                .description("practice swagger config")
                .version("1.0")
                .build();
    }
}

 

3. Swagger-ui ์ ‘์†ํ•˜์—ฌ ํ™•์ธ

  • Swagger2: http://localhost:8080/swagger-ui.html
  • Swagger3: http://localhost:8080/swagger-ui/index.html

 

4. Controller์— API ์ž‘์„ฑํ•˜๊ณ  ์„ค๋ช… ์ถ”๊ฐ€

@RestController
public class BasicController {
    @PostMapping("/api/member")
    @ApiOperation(value = "์‚ฌ์šฉ์ž ๋ชฉ๋ก ์กฐํšŒ", notes = "๊ฐ€์ž…๋œ ์‚ฌ์šฉ์ž ๋ชฉ๋ก์„ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.")
    public List<String> getMemberList() {
    	List<String> memberList = Arrays.asList("anna", "tom", "grace");
        return memberList;
    }

    @GetMapping("/api/member/{id}")
    @ApiOperation(value = "์‚ฌ์šฉ์ž ์กฐํšŒ", notes = "ID๋กœ ์‚ฌ์šฉ์ž๋ฅผ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.")
    public String getMemberById(@PathVariable("id") String id) {
    	List<String> memberList = Arrays.asList("anna", "tom", "grace");
    	return memberList.stream()
    			.filter(item -> item.equals(id))
    			.findAny()
    			.orElse(null);
    }
}

 

5. ๋‹ค์–‘ํ•œ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ API ํ˜ธ์ถœํ•˜์—ฌ ํ…Œ์ŠคํŠธ

๋Œ“๊ธ€