Spring Boot Framework
[Swagger] API 문서 자동화 설정
헤르메스의날개
2023. 3. 30. 00:13
728x90
개발환경
Spring Boot 2.7.9
H2 2.1.214
p6spy 1.8.1
slf4j 1.7.36
lombok
devtools
postgresql
참고로
Swagger 의 버전별 call URL이 다릅니다.
2.x 버전 : http://localhost:9090/swagger-ui.html
3.x 버전 : http://localhost:9090/swagger-ui/index.html
Swagger2 2.9.2 버전 설정
build.gradle
// Default : 2.0.0.RELEASE
//implementation 'org.springframework.plugin:spring-plugin-core'
implementation 'org.springframework.plugin:spring-plugin-core:1.2.0.RELEASE'
// Swagger
implementation 'io.springfox:springfox-swagger2:2.9.2'
implementation 'io.springfox:springfox-swagger-ui:2.9.2'
SwaggerConfiguration.java
import java.util.ArrayList;
import java.util.List;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.hateoas.client.LinkDiscoverer;
import org.springframework.hateoas.client.LinkDiscoverers;
import org.springframework.hateoas.mediatype.collectionjson.CollectionJsonLinkDiscoverer;
import org.springframework.plugin.core.SimplePluginRegistry;
import org.springframework.validation.Errors;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
@Bean
public Docket swaggerApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(swaggerInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("octopus.backend"))
.paths(PathSelectors.any())
.build()
.useDefaultResponseMessages(false); // 기본으로 세팅되는 200,401,403,404 메시지를 표시 하지 않음
}
private ApiInfo swaggerInfo() {
return new ApiInfoBuilder()
.title("Octopus API Documentation")
.description("앱 개발시 사용되는 서버 API에 대한 연동 문서입니다")
.license("MIT License")
.licenseUrl("https://hermeslog.tistory.com/")
.version("1.0")
.build();
}
@Bean
public LinkDiscoverers discoverers() {
List<LinkDiscoverer> plugins = new ArrayList<>();
plugins.add(new CollectionJsonLinkDiscoverer());
return new LinkDiscoverers(SimplePluginRegistry.create(plugins));
}
}
Swagger2 3.0.0 버전 설정
build.gradle
// Default : 2.0.0.RELEASE
implementation 'org.springframework.plugin:spring-plugin-core:2.0.0.RELEASE'
implementation 'io.springfox:springfox-boot-starter:3.0.0'
implementation 'io.springfox:springfox-swagger-ui:3.0.0'
SwaggerConfiguration.java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.Errors;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
@Bean
public Docket swaggerApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(swaggerInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("octopus.backend"))
.paths(PathSelectors.any())
.build()
.useDefaultResponseMessages(false); // 기본으로 세팅되는 200,401,403,404 메시지를 표시 하지 않음
}
private ApiInfo swaggerInfo() {
return new ApiInfoBuilder()
.title("Octopus API Documentation")
.description("앱 개발시 사용되는 서버 API에 대한 연동 문서입니다")
.license("MIT License")
.licenseUrl("https://hermeslog.tistory.com/")
.version("1.0")
.build();
}
// @Bean
// public LinkDiscoverers discoverers() {
// List<LinkDiscoverer> plugins = new ArrayList<>();
// plugins.add(new CollectionJsonLinkDiscoverer());
//
// return new LinkDiscoverers(SimplePluginRegistry.create(plugins));
// }
}
참고
https://kim-jong-hyun.tistory.com/49
https://hermeslog.tistory.com/698
728x90