개발일기/Java

스프링부트 Swagger 사용법

징구짱 2023. 3. 9. 17:51
728x90

build.gradle dependencies에 추가 (수정 후 꼭 코끼리 누르기!)

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

 

application.properties에 추가

spring.mvc.pathmatch.matching-strategy=ant_path_matcher

 

config 패키지 > SwaggerConfig만들기

 

@Configuration
public class SwaggerConfig {

    // http://localhost:8080/swagger-ui/index.html
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.OAS_30)
                .useDefaultResponseMessages(false)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.hanghaerolepost.controller"))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Swagger Test")
                .description("SwaggerConfig")
                .version("3.0")
                .build();
    }

}

apis에 controller주소 적기

 

(필수 사항은 아니지만) api에 설명 적기

@ApiOperation(value = "게시글 조회", notes = "전체 게시글을 조회한다.")

 

서버 실행 후 아래 링크 들어가기

http://localhost:8080/swagger-ui/index.html

 

테스트도 가능하다!

728x90