Zayson
A to Zayson!
Zayson
전체 방문자
오늘
어제
  • 분류 전체보기 (132)
    • Computer Science (20)
      • Network (4)
      • DB (12)
      • OS (4)
    • Algorithm (32)
      • 완전탐색(Brute-Force) (3)
      • 그리디(Greedy) (6)
      • 투포인터(Two-Pointer) (1)
      • 그래프(Graph) (5)
      • BFS & DFS (9)
      • 구현, 시뮬레이션(Implementation) (5)
      • 다이나믹 프로그래밍(DP) (3)
    • Backend (51)
      • Spring Boot (19)
      • JPA (16)
      • Kafka (2)
      • Java (13)
      • Kotlin (1)
    • DevOps (1)
      • Jenkins (5)
      • Oracle Cloud Infrastructure (1)
      • Kubernetes & Docker (1)
    • Trouble Shooting (3)
      • JPA (1)
      • Spring Boot (2)
    • 회고 (5)
      • 엔빵 프로젝트 포스트 로드맵 (1)
      • 2022년 (4)
    • Kafka (7)
      • Kafka (5)
      • Kafka Connect (2)
    • 기술 서적 (6)
      • 데이터 중심 애플리케이션 설계 (3)
      • 개발자가 반드시 정복해야할 객체 지향과 디자인 패턴 (2)
      • 가상 면접 사례로 배우는 대규모 시스템 설계 기초 (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

인기 글

태그

  • kafka
  • Kafka Connect
  • Java
  • 완전탐색
  • CS
  • 나 혼자 스프링부트!
  • JPA
  • 관계형 데이터베이스 실전 입문
  • spring boot
  • 프로그래머스
  • 라이브스터디
  • Computer science
  • SpringBoot
  • 엔빵프로젝트
  • 그리디
  • 백준
  • 구현
  • BFS
  • dfs
  • Backend

최근 글

티스토리

hELLO · Designed By 정상우.
Zayson

A to Zayson!

Swagger를 이용한 API 문서화
Backend/Spring Boot

Swagger를 이용한 API 문서화

2022. 4. 28. 18:11

❓Swagger란?

Swagger는 Open API를 명세하기 위한 Open Api Specification(OAS) 프레임워크이다. API들이 가진 스펙을 명세하고, 관리하여 문서화 해주는 기능을 가지고 있다.

Swagger를 이용해 다른 개발팀과 협업, 프로젝트의 유지보수 , 백엔드 API 명세서를 작성해 보다 편리하게 관리하는 것이 가능하다.

⚙️ Spring Boot Swagger 설정

Spring Boot 프로젝트에 의존성 추가를 한다.

implementation 'io.springfox:springfox-swagger2:2.9.2'    #Swagger 애노테이션 사용가능
implementation 'io.springfox:springfox-swagger-ui:2.9.2'  #Swagger UI 사용가능 

프로젝트의 Config 패키지에 Configuration 클래스를 생성한다.

package com.yourecipe.member.config;

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;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()                                       
                .apis(RequestHandlerSelectors.any())            // Swagger API 문서로 만들기 원하는 basePackage 경로
                .paths(PathSelectors.any())                     // apis() 중에 path에 맞는 API 필터링 
                .build();
    }

    // Swagger로 설정하는 API 정보
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("YouRecipe Member API")
                .description("YouRecipe Member API Reference for Developers")
                .termsOfServiceUrl("https://github.com/jymaeng95/YouRecipe-Member")
                .license("@Zayson License")
                .licenseUrl("http://you-recipe.com").version("1.0").build();
    }
}

❗Swagger 주요 애노테이션

  • @Api(tags = "", value = "") : Swagger 리소스임을 명시
  • @Api(tags = "Member", value = "회원 컨트롤러") public class MemberController {...}
  • @ApiOperation(value = "") : 한개의 Operation을 명세
  • @ApiOperation(value = "회원 가입") @PostMapping public ResponseEntity<String> signUpMember(@RequestBody Member member) {...}
  • @ApiModel : 모델 객체 명세
  • @ApiModel(description = "회원 정보를 위한 도메인 객체") public class Member {...}
  • @ApiModelProperty : 모델 객체 속성 명세
  • @ApiModelProperty(value = "회원 ID") private int memberId;

🔗 참고

https://doozi316.github.io/web/2020/10/16/WEB29/

반응형
저작자표시 비영리 변경금지 (새창열림)

'Backend > Spring Boot' 카테고리의 다른 글

싱글톤 스코프, 프로토타입 스코프  (0) 2022.05.03
Spring Data JPA의 Page와 Slice  (0) 2022.05.02
Spring Data JPA를 이용해 커서 페이징 구현하기  (0) 2022.05.01
Spring Cloud Config Server를 Private Repository와 연동  (0) 2022.04.28
Spring Cloud Config Server/Client 설정  (0) 2022.04.28
    'Backend/Spring Boot' 카테고리의 다른 글
    • Spring Data JPA의 Page와 Slice
    • Spring Data JPA를 이용해 커서 페이징 구현하기
    • Spring Cloud Config Server를 Private Repository와 연동
    • Spring Cloud Config Server/Client 설정
    Zayson
    Zayson
    공부한 내용을 정리하는 공간

    티스토리툴바