본문 바로가기
개발/Spring

[SpringBoot] Swagger 적용하기(SpringBoot 3.x.x 이상)

by 코딩하는 흰둥이 2024. 7. 5.
dependency 
<!-- swagger Maven -->
<dependency>
   <groupId>org.springdoc</groupId>
   <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
   <version>2.0.2</version>
</dependency>


<!-- swagger Gradle -->
dependencies {
	implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.2'
}

 

 

http://주소:포트/swagger-ui/index.html 로 접속하게 되면 

 

Swagger 화면으로 접속되는데

위의 화면처럼 나온다면

아래 경로를 치고 들어간다

입력 후 Explore

 

 

 

 

 

정상적인 접속

 

 

 

SwaggerConfig
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.info.Info;
import org.springframework.context.annotation.Configuration;

@OpenAPIDefinition(
        info = @Info(title = "Test API" ,
                description = "Swagger 확인용 문서",
                version = "V1.0"
        ))

@Configuration
public class SwaggerConfig {
}

 

 

Controller
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Tag(name="Swagger API 테스트", description = "TEST 1")
@Controller
@RequestMapping("/testtest/*")
public class SwaggerTestController {

    @Operation(summary = "정보 가져오기", description = "Controller에 입력한 데이터를 불러온다")
    @ResponseBody
    @GetMapping("/swaggerGetData")
    public String swaggerGetData() throws Exception {
        return "제대로 전송되는겨?";
    }


    @Operation(summary = "사용자가 입력한 정보 가져오기", description = "사용자가 입력한 데이터를 불러온다")
    @ResponseBody
    @GetMapping("/swaggerInputData")
    public String swaggerInputData(@RequestParam String test1) throws Exception {
        return test1;
    }

}

 

 

 

Vo - Schema
import io.swagger.v3.oas.annotations.media.Schema;

@Schema(description = "BoardVo 정보")
public class BoardVo {
    private Integer b_num;
    
    @Schema(description = "테스트 이름", nullable = false)
    private String b_name;

    public Integer getB_num() {
        return b_num;
    }

    public void setB_num(Integer b_num) {
        this.b_num = b_num;
    }

    public String getB_name() {
        return b_name;
    }

    public void setB_name(String b_name) {
        this.b_name = b_name;
    }

}

 

 

 

 

다시 http://주소:포트/swagger-ui/index.html 로 접속하게 되면 

설정한 대로 페이지가 구성된다

 

 

 

TEST

 

 

 

Schema를 설정한 부분에만 설명이 들어가 있다

댓글