반응형

Spring Boot : Swagger UI 활용 (3) - Controller 테스트

spring fox 라이브러리의 Swagger UI를 활용하여 spring boot 프로젝트에 매핑된 controller 테스트

 

  • Swagger : 정의, 활용
  • 개발환경 : Java, Maven, Spring-fox
  • 라이브러리 다운로드 (pom.xml)
  • 설정 : application.properties / application.yml
  • Hello World : 기본 호출 테스트 - default controller
  • swagger 설정 추가
  • 문자열 List 반환 테스트

 

 

# swagger 설정 추가

 

[ SwaggerConfiguration.java ]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package home.test.springfox.swagger;
 
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;
 
@Configuration
@EnableSwagger2 // Enables Springfox swagger 2
public class SwaggerConfiguration {
 
    @Bean
    public Docket SampleTestApi() {
        return new Docket(DocumentationType.SWAGGER_2)  // Docket, Springfox's, primary api configuration mechanism is initialized for swagger specification 2.0
                .select()                               // select() returns an instance of ApiSelectorBuilder to give fine grained control over the endpoints exposed via swagger.
                .apis(RequestHandlerSelectors.any())    // apis() allows selection of RequestHandler's using a predicate. The example here uses an any predicate (default). Out of the box predicates provided are any, none, withClassAnnotation, withMethodAnnotation and basePackage.
                .paths(PathSelectors.any())             // paths() allows selection of Path's using a predicate.
                .build()                                // The selector needs to be built after configuring the api and path selectors.
                .apiInfo(getApiInfo());
    }
 
    private ApiInfo getApiInfo() {
        return new ApiInfoBuilder()
                .title("Sample Test API")
                .version("0.0.1")
                .description("API for basic CRUD for Sample Test")
                .contact(new Contact("D""https://tlo-developer.tistory.com""tester@test.com"))
                .license("Apache License Version 2.0")
                .build();
    }
}
 

 

 

[ SwaggerUiWebMvcConfigurer.java ]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package home.test.springfox.security;
 
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 
public class SwaggerUiWebMvcConfigurer implements WebMvcConfigurer {
 
    private final String baseUrl;
 
    public SwaggerUiWebMvcConfigurer(String baseUrl) {
        this.baseUrl = baseUrl;
    }
 
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String baseUrl = StringUtils.trimTrailingCharacter(this.baseUrl, '/');
        registry.addResourceHandler(baseUrl + "/swagger-ui/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/")
                .resourceChain(false);
    }
 
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController(baseUrl + "/swagger-ui/")
                .setViewName("forward:" + baseUrl + "/swagger-ui/index.html");
    }
 
}
 

 

 

[ TestSpringfoxApplication.java ] @Import(SwaggerConfiguration.class) 추가

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package home.test.springfox;
 
import home.test.springfox.swagger.SwaggerConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Import;
 
@Import(SwaggerConfiguration.class)
@SpringBootApplication
public class TestSpringfoxApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(TestSpringfoxApplication.class, args);
    }
 
}
 

 

 

 

 


참고

Springfox Reference Documentation

 

반응형