我正在尝试将 swagger 2 集成到我的 spring-mvc webapp(没有 spring boot)中。
当我转到/myApp/v2/api-odcs 时它起作用了,我看到了版本、描述和我所有的 restControllers。
但是当我尝试访问/myApp/swagger-ui.html 时,我得到的只是一个空白页面,甚至没有绿色导航栏。
当我监控网络时,我看到它成功获取了所有资源(swagger-ui.html、css 和 js)(状态 200),并在选项卡中显示标题:“MyApp:Swagger UI”。
这是我的配置:
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
@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/");
}
}
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
.paths(PathSelectors.any())
.build()
.pathMapping("/")
.apiInfo(apiInfo())
.useDefaultResponseMessages(false);
}
@Bean
public ApiInfo apiInfo() {
final ApiInfoBuilder builder = new ApiInfoBuilder();
builder
.title("MyApp - API")
.description("desc")
.version("1.0");
return builder.build();
}
}
这是我的依赖项:
compile group: 'org.webjars', name: 'swagger-ui', version: '3.5.0'
compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.7.0'
compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.7.0'
最佳答案
这是我的配置,如果有帮助的话。我还指定了主机和路径提供程序。
@Configuration
@EnableSwagger2
@Profile({"!production"})
public class SwaggerConfiguration extends WebMvcConfigurerAdapter {
@Autowired
private ServletContext servletContext;
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.host("localhost")
.directModelSubstitute(LocalDate.class, Date.class)
.pathProvider(new RelativePathProvider(servletContext) {
@Override
public String getApplicationBasePath() {
return "/myApp";
}
})
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
注意:如果这是一个 SpringMVC 应用程序,您可能还需要删除 @EnableWebMvc
关于java - 带有 spring mvc 的 swagger ui 空白页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49921042/