[Error] Swagger 2.9.2 /swagger-ui.html 열리지 않을때 해결

GilLog·2021년 7월 7일
0

에러

목록 보기
14/17
post-custom-banner

상황

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를 담당하는 ClassWebMvcConfigurationSupport를 상속받은 후,

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);
	}
}
profile
🚀 기록보단 길록을 20.10 ~ 22.02 ⭐ Move To : https://gil-log.github.io/
post-custom-banner

0개의 댓글