ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [ spring boot ] swagger(스웨거) 간단한 사용 방법!
    Web/Spring boot 2019. 11. 15. 22:57

    스웩넘치는 swagger

    흠... postman과 같은 기능을 한다.

     

    pom.xml추가

    <properties>
    	<java.version>1.8</java.version>
    	<swagger.version>2.8.0</swagger.version>
    </properties>
    
    <!-- swagger -->
    		<dependency>
    			<groupId>io.springfox</groupId>
    			<artifactId>springfox-swagger2</artifactId>
    			<version>${swagger.version}</version>
    		</dependency>
    		<dependency>
    			<groupId>io.springfox</groupId>
    			<artifactId>springfox-swagger-ui</artifactId>
    			<version>${swagger.version}</version>
    		</dependency>
    		<dependency>
    			<groupId>io.springfox</groupId>
    			<artifactId>springfox-spi</artifactId>
    			<version>${swagger.version}</version>
    		</dependency>
    		<dependency>
    			<groupId>io.springfox</groupId>
    			<artifactId>springfox-bean-validators</artifactId>
    			<version>${swagger.version}</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-web</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-web-services</artifactId>
    		</dependency>

    conf 패키지 안에 SwaggerConfig class 생성

    * 주의:

    basePackage(" controller가 들어있는 패키지 " )를 써줘야 한다.

     

    package com.siris.api.conf;
    
    import java.time.LocalDate;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.http.ResponseEntity;
    
    import com.google.common.base.Predicate;
    
    import springfox.documentation.builders.RequestHandlerSelectors;
    import springfox.documentation.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    import static com.google.common.base.Predicates.*;
    import static com.google.common.collect.Lists.*;
    import static springfox.documentation.builders.PathSelectors.*;
    
    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {
    	
    	@Bean
    	public Docket api(){		
    		return new Docket(DocumentationType.SWAGGER_2)  
    		          .select()                                  
    		          //.apis(RequestHandlerSelectors.any())	//현재 RequestMapping으로 할당된 모든 URL 리스트를 추출              
    		          .apis(RequestHandlerSelectors.basePackage("com.siris.api"))	
    		          .paths(opPaths())               //필터링 ex) 여기서는 /api/*.*           
    		          .build()		          
    		          .directModelSubstitute(LocalDate.class, String.class)		          
    		          .genericModelSubstitutes(ResponseEntity.class);
    	}
    	
    	private Predicate<String> opPaths() {
    		return or(
    				regex("/*.*")
    				);
    	}
    }
    

     

    프로젝트 실행 후 http://localhost:18888/swagger-ui.html 를 들어가면 내가 컨트롤러에서 만든 메소드들이 나온다.

    각 메소드를 눌러 값을 넣고 출력하는것도 잘 나옴

     

    반응형

    댓글

Designed by Tistory.