개발환경

Swagger UI 사용법

JUN0126 2022. 7. 6. 11:04

Swagger UI 란?

 - Swagger 제품 중 RestAPI Controller를 자동으로 API Document를 시각화하여 보여줄 수 있는 Open Source이다

 또한 Controller를 실행하여 테스트를 할 수 있으며 요청값을 전달하고 응답을 받을 수 있습니다. (Postman 과 같은 기능)

 

Dependency 추가

1. Gradle

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

2. Maven

<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>

 

Config 설정

@Configuration
@EnableWebMvc
public class SwaggerConfig {

    private ApiInfo swaggerInfo() {
        return new ApiInfoBuilder().title("Swagger UI 화면 제목")
                .description("Swagger UI 화면 제목").build();
    }

    @Bean
    public Docket swaggerApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .consumes(getConsumeContentTypes())
                .produces(getProduceContentTypes())
                .apiInfo(swaggerInfo()).select()
                .apis(RequestHandlerSelectors.basePackage("controller 패키지 경로")) // EX) com.lecture.api.controller
                .paths(PathSelectors.any())
                .build()
                .useDefaultResponseMessages(false);
    }

    // Swagger UI를 보여주기 위한 설정 값 swaggerApi 메소드에서 사용됨
    private Set<String> getConsumeContentTypes() {
        Set<String> consumes = new HashSet<>();
        consumes.add("application/json;charset=UTF-8");
        consumes.add("application/x-www-form-urlencoded");
        return consumes;
    }

    private Set<String> getProduceContentTypes() {
        Set<String> produces = new HashSet<>();
        produces.add("application/json;charset=UTF-8");
        return produces;
    }
}

 

UI 표기 제외 방법

- RestController 클래스에서 @ApiIgnore 어노테이션 선언 시 해당 Controller는 Swagger UI에 포함되지 않게 됩니다. 

 

 

Swagger UI 화면 경로

 - 기본적으로 "서버 주소/swagger-ui/index.html" 를 통하여 접근 가능하다.

 

 

참고 : https://nahwasa.com/entry/%EC%8A%A4%ED%94%84%EB%A7%81%EB%B6%80%ED%8A%B8-Swagger-UI-300-%EC%A0%81%EC%9A%A9-%EB%B0%A9%EB%B2%95-%EC%8A%A4%ED%94%84%EB%A7%81%EB%B6%80%ED%8A%B8-22-%EC%9D%B4%EC%83%81-Spring-Boot-Swagger-UI

 

스프링부트 Swagger UI 3.0.0 적용 방법 - 스프링부트 2.2 이상 (Spring Boot Swagger UI)

Spring Boot Swagger 3.0.0 적용하기 (스프링부트 2.2 이상 필요) Swagger ? 간단히 말하자면 API 문서를 자동으로 만들어주는 라이브러리임 https://swagger.io/ 예시는 스웨거의 Live Demo 참조 (https://petst..

nahwasa.com

 

https://sharplee7.tistory.com/48

 

Swagger UI 사용법

정의 Swagger UI란 Swagger 제품군 중 API Documentation과 관련된 기능을 제공하는 제품이다. Swagger 홈페이지에 등록된 Swagger UI에 대한 설명은 다음과 같다. Swagger UI allows anyone — be it your develo..

sharplee7.tistory.com

 

'개발환경' 카테고리의 다른 글

야후 파이낸셜 API 인증  (0) 2022.02.04
HTTP 와 HTTPS 개념 및 차이점  (0) 2021.12.30
Git관리 - 3  (0) 2021.07.28
Git관리 - 2  (0) 2021.07.28
Git 관리 - 1  (0) 2021.07.27