swagger ui空白页面与春天mvc

时间:2018-04-19 12:22:56

标签: java rest spring-mvc swagger swagger-ui

我试图在我的spring-mvc webapp(无弹簧启动)中集成swagger 2。

当我转到/ myApp / v2 / api-odcs时,它会工作,我看到版本,描述和所有我的restControllers。

但是当我尝试访问/myApp/swagger-ui.html所有我得到的是一个空白页面,甚至没有绿色导航栏。

当我监控网络时,我看到它成功获取所有资源(swagger-ui.html,css和js)(状态200)并在选项卡中显示标题:" MyApp:Swagger UI&#34 ;

这是我的配置:

@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'

1 个答案:

答案 0 :(得分: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