java - 带有 spring mvc 的 swagger ui 空白页面

标签 java rest spring-mvc swagger swagger-ui

我正在尝试将 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

https://github.com/springfox/springfox/issues/776

关于java - 带有 spring mvc 的 swagger ui 空白页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49921042/

相关文章:

java - 单击提交按钮时,用户不会导航到 Selenium Webdriver 中的下一页

rest - 从 REST api 获取 Jenkins 工件 URl

java - 如何使用 Spring MVC 覆盖 Jetty 的 404 错误?

spring - 使用 EnableWebSecurity 时 AuthenticationPrincipal 为空

java - spring mvc 表单错误没有出现在 jsp 上

Java 代码约定。将对象与方法混合?

java - 从大型机批处理程序调用 Web 服务

java - Mac OS X Java SDK 支持

c# - Web 服务调用时长

scala - 在 Gatling 场景和模拟之间传递参数