여러가지 Swagger의 integration tool이 있는데,
이번 예제에서는 spring fox를 사용해보도록 하겠다.
기본적으로 springfox에서는 서버에 설정되어 있는 controller의 request와 response를 파싱하고,
해당 모델들을 json 형식으로 만들어 /v2/api-docs 를 호출하게 되면 응답으로 내려주게 된다.
이에 더해 spring fox에서는 swagger-ui 역시 내장시킬 수 있다.
먼저 아래와 같이 EnableSwagger2 어노테이션을 설정함으로써 해당 /v2/api-docs를 enable할 수 있다.
package org.blog.test.config;
import com.fasterxml.classmate.TypeResolver;
import org.springframework.beans.factory.annotation.Autowired;
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;
/**
* Created by ugeun.park on 2017-06-17.
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Autowired
private TypeResolver typeResolver;
@Bean
public Docket swaggerApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.regex("/health/*"))
.build();
}
}
아래와 같이 springfox-swagger-ui를 dependency로 설정함으로써, 자체 서버 내장 swagger-ui를 사용할 수 있다.
compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.7.0'
- 실행 결과
전체 예제는 아래 링크에서 다운로드 받을 수 있다.
https://gitlab.com/shashaka/spring-swagger-project

댓글 없음 :
댓글 쓰기