header-img
Info :
728x90
dev. environment.
SpringBoot 3.1.1
Java 17

μŠ€μ›¨κ±°(Swagger) λž€?

  • κ°œλ°œμžκ°€ REST API μ„œλΉ„μŠ€λ₯Ό 섀계, λΉŒλ“œ, λ¬Έμ„œν™”ν•  수 μžˆλ„λ‘ ν•˜λŠ” ν”„λ‘œμ νŠΈ
  • REST APIλ₯Ό λ¬Έμ„œν™”ν•˜λŠ” 도ꡬ이며, API에 λŒ€ν•œ λͺ…μ„Έ(Spec)을 κ΄€λ¦¬ν•˜κΈ° μœ„ν•œ ν”„λ‘œμ νŠΈ
  • APIκ°€ μˆ˜μ •λ˜λ”λΌλ„ λ¬Έμ„œκ°€ μžλ™μœΌλ‘œ κ°±μ‹ . 
  • 2011 년에 처음 λ„μž…λœ κ°œλ…

κΈ°μ‘΄, μ›Œλ“œλ‚˜ μ—‘μ…€ λ“± 수기 μž‘μ„±ν•˜λŠ” λ¬Έμ„œν˜• API λͺ…μ„Έμ„œμ™€ 달리 μ„œλ²„μ— 일정 μ…‹νŒ…μ„ ν•΄μ£Όλ©΄ API λͺ…μ„Έμ„œκ°€ κ°±μ‹ λœλ‹€.

 

λ°±μ—”λ“œ 개발자의 경우 ν΄λΌμ΄μ–ΈνŠΈλ₯Ό κ°œλ°œν•˜λŠ” ν”„λ‘ νŠΈμ—”λ“œ κ°œλ°œμžμ™€ μ†Œν†΅μ„ ν•˜λ €λ©΄ API λͺ…μ„Έμ„œκ°€ κΌ­ ν•„μš”ν•˜κ²Œ λ˜λŠ”λ°

이걸 ν•˜λ‚˜ν•˜λ‚˜ λͺ…μ„Έν•˜κ³  μžˆλŠ” 것도 κ½€λ‚˜ 큰 일둜 νŒŒμ•…λœλ‹€.

이런 업무 둜슀λ₯Ό 쀄이기 μœ„ν•΄ μ„œλ²„ 단에 μ μš©ν•˜λŠ” 것이 λ°”λ‘œλ°”λ‘œ μŠ€μ›¨κ±°λ‹€.

 

 

API Documentation & Design Tools for Teams | Swagger

Loved by all • Big & Small Thousands of teams worldwide trust Swagger to deliver better products, faster.

swagger.io

 

μŠ€μ›¨κ±°(Swagger)의 μ’…λ₯˜

Spring-Fox 

  • 2015 년에 λ‚˜μ˜¨ 라이브러리
  • 2020λ…„ 7μ›” 3.0.0 버전을 λ§ˆμ§€λ§‰μœΌλ‘œ μ—…λ°μ΄νŠΈκ°€ 멈좀

Spring-Dox

  • 2019λ…„ 7월에 λ‚˜μ˜¨ 라이브러리 - SpringFoxκ°€ μ—…λ°μ΄νŠΈλ₯Ό μ€‘λ‹¨ν–ˆλ˜ μ‹œμ ˆ..
  • 2023λ…„ 4μ›”κΉŒμ§€ μ—…λ°μ΄νŠΈκ°€ μ§„ν–‰λ˜κ³  μžˆλ‹€. (23.10 κΈ°μ€€)
  • webfluxλΌλŠ” λ…Ό λΈ”λ‘œν‚Ή 비동기 λ°©μ‹μ˜ μ›Ή κ°œλ°œμ„ μ§€μ›ν•˜λ„λ‘ 개발됨

고둜 μ΅œμ‹  λ²„μ „μ˜ springboot 의 경우 spring-doc을 μ‚¬μš©ν•˜λŠ” 것이 μš©μ΄ν•˜λ‹€

 

 

SpringDoc을 μ‚¬μš©ν•œ Swagger 적용

1. 라이브러리 μΆ”κ°€

swagger κ΄€λ ¨ 라이브러리의 경우 버전 확인이 κΌ­ ν•„μš”ν•˜λ‹€,

2.x.x 버전과 3.0.0버전이 swagger uiλ₯Ό 뢈러올 λ•Œ μ‚¬μš©ν•˜λŠ” URL이 λ‹€λ₯΄κΈ° λ•Œλ¬Έ.

....
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
....
      <dependency>
          <groupId>io.springfox</groupId>
          <artifactId>springfox-boot-starter</artifactId>
          <version>3.0.0</version>
      </dependency>
      <dependency>
          <groupId>io.springfox</groupId>
          <artifactId>springfox-swagger-ui</artifactId>
          <version>3.0.0</version>
      </dependency>

.....

2. μ½”λ“œ μž‘μ„±

SwaggerConfig.java 파일과 TestController.java νŒŒμΌμ„ μƒμ„±ν•΄λ³΄μž.

 

SwaggerConfig.java

package com.example.swagger.demo;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * The Class SwaggerConfig.
 */
 
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket api(){
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build().apiInfo(apiInfo());

    }

    private ApiInfo apiInfo() {
        String description = "Welcome Log Company";
        return new ApiInfoBuilder()
                .title("SWAGGER TEST")
                .description(description)
                .version("1.0")
                .build();
    }

}

TestController.java

package com.example.swagger.demo;

import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * The Class TestController
 */
@RestController
public class TestController {

    @GetMapping(value = "/hello")
    @ApiOperation(value = "hello, world api", notes = "hellow world swagger check")
    public String hellowWorld(){
        return "hello, world";
    }


    @ApiOperation(value = "test", notes = "ν…ŒμŠ€νŠΈμž…λ‹ˆλ‹€")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok"),
            @ApiResponse(code = 404, message = "page not found!")
    })
    @GetMapping(value = "/board")
    public Map<String, String> selectBoard(@ApiParam(value = "μƒ˜ν”Œλ²ˆν˜Έ", required = true, example = "1")
                                           @RequestParam String no) {

        Map<String, String> result = new HashMap<>();
        result.put("test title", "ν…ŒμŠ€νŠΈ");
        result.put("test contents", "ν…ŒμŠ€νŠΈ λ‚΄μš©");
        return  result;
    }
}

3. Swagger UI 확인

Swagger 버전에 λ”°λ₯Έ URL
2.x.x : http://localhost:8080/swagger-ui.html
2.x.x : http://localhost:8080/swagger-ui/index.html

ν•΄λ‹Ή URL에 λ“€μ–΄κ°€κ²Œ 되면.. μ•„λž˜μ™€ 같이 ν…ŒμŠ€νŠΈ 화면이 λ‚˜νƒ€λ‚˜κ²Œ λœλ‹€.

 

728x90
더보기
BACKEND/SpringBoot