pom.xml
에 Spring Swagger 2.9.2 관련 Dependency를 추가하고,
SwaggerConfig
관련 Class를 생성하여 Server 실행 시 정상 동작하지만,
localhost/swagger-ui.html
로 Swagger 문서 접속이 안되는 상황이었다.
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).consumes(getConsumeContentTypes())
.produces(getProduceContentTypes())
.useDefaultResponseMessages(false)
.apiInfo(getApiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("("****************")"))
.paths(PathSelectors.ant("/**"))
.build();
}
private Set<String> getConsumeContentTypes() {
Set<String> consumes = new HashSet<>();
consumes.add("application/json;charset=UTF-8");
consumes.add("application/x-www-form-urlencoded");
return consumes;
}
private Set<String> getProduceContentTypes() {
Set<String> produces = new HashSet<>();
produces.add("application/json;charset=UTF-8");
return produces;
}
private ApiInfo getApiInfo() {
return new ApiInfoBuilder().title("("****************")")
.description("****************")
.contact(new Contact("("****************")", "("****************")", "("****************")"))
.version("1.0")
.build();
}
}
@EnableSwagger2
를 통해 Swagger Config
를 담당하는 Class에 WebMvcConfigurationSupport
를 상속받은 후,
addResourceHandlers
method를 override 해주면 된다.
@Configuration
@EnableSwagger2
public class SwaggerConfig extends WebMvcConfigurationSupport {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).consumes(getConsumeContentTypes())
.produces(getProduceContentTypes())
.useDefaultResponseMessages(false)
.apiInfo(getApiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("("****************")"))
.paths(PathSelectors.ant("/**"))
.build();
}
private Set<String> getConsumeContentTypes() {
Set<String> consumes = new HashSet<>();
consumes.add("application/json;charset=UTF-8");
consumes.add("application/x-www-form-urlencoded");
return consumes;
}
private Set<String> getProduceContentTypes() {
Set<String> produces = new HashSet<>();
produces.add("application/json;charset=UTF-8");
return produces;
}
private ApiInfo getApiInfo() {
return new ApiInfoBuilder().title("("****************")")
.description("****************")
.contact(new Contact("("****************")", "("****************")", "("****************")"))
.version("1.0")
.build();
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
super.addResourceHandlers(registry);
}
}