@Configuration
@EnableSwagger2
public class SwaggerConfig2 extends WebMvcConfigurerAdapter {
@Primary
@Bean
public SwaggerResourcesProvider swaggerResourcesProvider() {
return () -> {
SwaggerResource wsResource = new SwaggerResource();
wsResource.setName("Documentation");
wsResource.setSwaggerVersion("2.0");
wsResource.setLocation("/swagger.yaml");
List<SwaggerResource> resources = List.of(wsResource);
return resources;
};
}
@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/");
registry.addResourceHandler("/swagger.yaml").addResourceLocations("classpath:/swagger.yaml");
}
}
我正在尝试在 swagger 配置中添加我的 swagger.yaml 自定义文件以添加 swagger ui,但是当我调用 url“http://localhost:8078/swagger-ui.html#/”时,我得到“无法加载 API 定义。 “和“Fetch errorundefined http://localhost:8078/swagger.yaml” 那么如何解决这个问题呢?
@Configuration
@EnableSwagger2
public class SwaggerSpecConfig {
@Primary
@Bean
public SwaggerResourcesProvider swaggerResourcesProvider(
InMemorySwaggerResourcesProvider defaultResourcesProvider) {
return () -> {
List<SwaggerResource> resources = new ArrayList<>();
Arrays.asList("api1", "api2")
.forEach(resourceName -> resources.add(loadResource(resourceName)));
return resources;
};
}
private SwaggerResource loadResource(String resource) {
SwaggerResource wsResource = new SwaggerResource();
wsResource.setName(resource);
wsResource.setSwaggerVersion("2.0");
wsResource.setLocation("/swagger-apis/" + resource + "/swagger.yaml");
return wsResource;
}
@Bean
public Docket swagger() {
return new Docket(SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
"put your 'swagger.yaml' file in this folder'src/main/resources/swagger-apis/api1/swagger.yaml'"