뭐든지시작이반이다
Dream Future
뭐든지시작이반이다
  • 분류 전체보기
    • Spring
      • 개념
      • 기타
    • Java
    • Algorithm
      • 알고리즘 정리
    • DB
      • Postgresql
    • 트러블슈팅
    • Git & Github
    • V&V
    • EFK
    • 북스터디
    • 기타

인기 글

최근 댓글

전체 방문자
오늘
어제

블로그 메뉴

  • 홈
  • 태그
  • 방명록

태그

  • dirtyChecking
  • ambiguous오류
  • auditing
  • Kibana
  • github
  • Hibernate
  • gitignore
  • 대규모서비스
  • springboot
  • git
  • gradle
  • multimodule
  • mybatis
  • 파티셔닝
  • SqlSessionFactory
  • docker
  • spring
  • fluentd
  • Controller
  • lombok
  • 알고리즘
  • JPA
  • spring-boot
  • 트러블슈팅
  • sqlSessionTemplate
  • springbot
  • Kotlin
  • efk
  • SelectionSort
  • requestmapping
hELLO · Designed By 정상우.
뭐든지시작이반이다

Dream Future

[Spring boot] swagger 기본 설정
Spring/기타

[Spring boot] swagger 기본 설정

2022. 5. 29. 18:34

build.gradle dependencies에 다음 추가

dependencies {
	...
    
	// swagger ui
	implementation 'io.springfox:springfox-boot-starter:3.0.0'
	implementation 'io.springfox:springfox-swagger-ui:3.0.0'
}

 

SwaggerConfig.java

package com.example.simpleBoard;

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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@EnableSwagger2
@Configuration
public class SwaggerConfig {

    @Bean
    public Docket api(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo()) //api 문서에 대한 정보
                .select() //ApiSelectionBuilder 반환
                .apis(RequestHandlerSelectors.basePackage("com.example.simpleBoard")) //api 문서화 적용대상 패키지 설정 any(), none()
                .paths(PathSelectors.ant("/api/**")) //api 문서화 적용대상 url path 설정 any(), none()
                .build(); //springfox builder docket 생성
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("simpleBoard API")
                .description("간단한 게시판 기능 api")
                .contact(new Contact("seungmin","https://seungmin-archive.tistory.com",""))
                .version("1.0")
                .build();
    }
}

 

controller method에 ApiOperation으로 swagger 설정 적용

@RequiredArgsConstructor
@RestController
public class PostApiController {    
    
    ...
    
    @ApiOperation("post 등록")
    @PostMapping("api/v1/post")
    public Long savePost(@RequestBody PostSaveRequestDto requestDto){
        return postService.save(requestDto);
    }
    
    ...
}

 

spring boot 2.6 이상에서 필요한 설정

application.yml에 다음 추가

spring:
	mvc:
    		pathmatch: 
        		matching-strategy: ant_path_matcher

 

로컬 실행 시 다음 주소로 이동하여 확인: http://localhost:8080/swagger-ui/index.html

swagger ui 페이지

 

저작자표시 비영리 동일조건 (새창열림)

'Spring > 기타' 카테고리의 다른 글

[JPA/hibernate] Parameter value [\] did not match expected type [~] 오류 해결방법  (0) 2022.05.15
[Spring boot] mybatis 설정 sqlsession 관련 설정이 필요없는 이유  (0) 2022.05.09
Error executing DDL 오류 해결방법  (0) 2022.02.07
gradle 전체 테스트 수행 시 test events were not received 문구  (0) 2022.02.06
IntelliJ 프로젝트 JDK 버전 변경하기  (0) 2022.01.13
    'Spring/기타' 카테고리의 다른 글
    • [JPA/hibernate] Parameter value [\] did not match expected type [~] 오류 해결방법
    • [Spring boot] mybatis 설정 sqlsession 관련 설정이 필요없는 이유
    • Error executing DDL 오류 해결방법
    • gradle 전체 테스트 수행 시 test events were not received 문구
    뭐든지시작이반이다
    뭐든지시작이반이다
    기록장입니다.

    티스토리툴바