ARCHIVES

태그

신고하기

상단 메뉴 페이지

기본 콘텐츠로 건너뛰기

스프링부트 with JPA 블로그, Swagger 1부

동영상 강의 73강 완료 후

api 만 추출해서 Swagger를 만들어 보자. 

현재 최신버전은 3.0.0 이다.

그러나 2.6.1 버전이 딱 필요한 정보만 있으면서, 테스트가 편리하기 때문에

일단 이걸로 진행한다. 


1. pom.xml  파일에 스웨거 설정 추가

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.6.1</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.6.1</version>
</dependency>

2. SwaggerConfig.java 파일 생성

package com.cos.blog.config;


import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;


import springfox.documentation.builders.PathSelectors;

import springfox.documentation.builders.RequestHandlerSelectors;

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()) // 현재 RequestMapping으로 할당된 모든 URL 리스트추출

          //.paths(PathSelectors.any()) // 전체

          .paths(PathSelectors.ant("/api/**")) // 그중 /api/** 인 URL들만 필터링

          .build();

    }

}

3. SecurityConfig.java 를 설정 했다면 리소스 필터를 추가 한다.

@Override

public void configure(WebSecurity web) throws Exception {

    // swagger 관련 리소스 시큐리티 필터 제거

    web.ignoring().antMatchers(

            "/v2/api-docs", "/swagger-resources/**",

            "/swagger-ui.html", "/webjars/**", "/swagger/**");

}

4. 이제 접속 해보자. 

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


이제 상세 메세지는 공부해서 추가 해보자.




댓글