1.build.gradle에 의존성 추가
implementation group: 'io.springfox', name: 'springfox-boot-starter', version: '3.0.0'
implementation group: 'io.springfox', name: 'springfox-swagger-ui', version: '3.0.0'
[solved] Unable to infer base url. This is common when using dynamic servlet registration or when the API is behind an API Gateway. The base url is the root of where all the swagger resources are served. For e.g. if the api is available at http://example.org/api/v2/api-docs then the base url is http://example.org/api/. Please enter the location manually:
에러 해결
package config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
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;
@Configuration
public class SwaggerConfig extends WebMvcConfigurationSupport {
@Bean
public Docket api() {
return new Docket(DocumentationType.OAS_30) // 3.0 문서버전으로 세팅
.useDefaultResponseMessages(true)
.apiInfo(apiInfo()) // Swagger API 문서에 대한 설명 표기
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.xxxx")) // .apis()는 Swagger API 문서로 만들기 원하는 basePackage 경로 (필수)
.paths(PathSelectors.any())// 해당 URL에 해당하는 요청만 Swagger API 문서로 만듬. any: 전부 만약, ex. member/ -> member로 시작하는 api
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Swagger 3.0 Api Sample")
.description("This is Sample")
.version("1.0")
.build();
}
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
3.
http://localhost:8080/swagger-ui/index.html