【问题标题】:WhiteLabel error page while trying to access swagger-ui.html using springfox configuration尝试使用 springfox 配置访问 swagger-ui.html 时出现 WhiteLabel 错误页面
【发布时间】:2018-12-03 20:23:05
【问题描述】:

我正在尝试使用 Springfox 并遵循 https://www.baeldung.com/swagger-2-documentation-for-spring-rest-api 文档从 springboot 项目生成 Swagger 文档。

最初我收到错误“访问资源需要完全授权”,因为我在我的应用程序中使用 OAuth2。我更改了配置以允许所有以 /swagger-ui.html 结尾的请求。

现在我在尝试访问本地的 /swagger-ui.html 时收到“WhiteLabel 错误页面 - 此应用程序没有明确的 /error 映射”。

我浏览了各种帖子,但没有一个解决方案对我有用——我没有使用可能会干扰的@webmvcconfiguration。 有人可以帮忙吗?

【问题讨论】:

    标签: spring-boot swagger springfox


    【解决方案1】:

    对于 Swagger 3.0,URL 更改为

    http://localhost:8080/swagger-ui/index.html

    【讨论】:

      【解决方案2】:
      1. 从 pom.xml 中删除 v2 依赖项或将它们注释掉。

         <dependency>
             <groupId>io.springfox</groupId>
             <artifactId>springfox-swagger2</artifactId>
             <version>3.0.0</version>
         </dependency>
        
         <dependency>
             <groupId>io.springfox</groupId>
             <artifactId>springfox-swagger-ui</artifactId>
             <version>3.0.0</version>
         </dependency>
        
      2. 添加 springfox-boot-starter

         <dependency>
             <groupId>io.springfox</groupId>
             <artifactId>springfox-boot-starter</artifactId>
             <version>3.0.0</version>
         </dependency>
        
      3. 将浏览器中的 URL 更改为: http://localhost:8080/swagger-ui/index.html

      URL 的一般形式是:

      • http://host/context-path/swagger-ui/index.html 或
      • http://host/context-path/swagger-ui/

      有关更多信息,请查看指向相关文档的链接: https://springfox.github.io/springfox/docs/snapshot/#changes-in-swagger-ui

      【讨论】:

        【解决方案3】:

        这就是我解决问题的方法。这是我的详细代码,如果有人想看。

        https://github.com/xbox2204/SpringBoot-JPA-Swagger

        现在,我使用了 3.0.0-SNAPSHOT 和我从这里创建的最新 spring-boot 启动项目:

        https://start.spring.io/

        1. 我的 pom.xml,我添加了以下依赖项:
            <dependency>
                    <groupId>io.springfox</groupId>
                    <artifactId>springfox-swagger2</artifactId>
                    <version>3.0.0-SNAPSHOT</version>
                    <exclusions>
                        <exclusion>
                            <groupId>org.springframework.plugin</groupId>
                            <artifactId>spring-plugin-core</artifactId>
                        </exclusion>
                    </exclusions>
                </dependency>
                <dependency>
                    <groupId>io.springfox</groupId>
                    <artifactId>springfox-swagger-ui</artifactId>
                    <version>3.0.0-SNAPSHOT</version>
                </dependency>
                <dependency>
                    <groupId>io.springfox</groupId>
                    <artifactId>springfox-boot-starter</artifactId>
                    <version>3.0.0-SNAPSHOT</version>
                </dependency>
                <dependency>
                    <groupId>org.springframework.plugin</groupId>
                    <artifactId>spring-plugin-core</artifactId>
                    <version>2.0.0.RELEASE</version>
                </dependency>
        
        
        1. 在我的 application.properties 中,我添加了以下内容:
        spring.resources.add-mappings=true
        
        1. 在我的 SpringBoot Main/Runner 类中,我添加了这些注释
        @EnableWebMvc
        @EnableSwagger2
        @SpringBootApplication
        
        1. 我的 Docket 返回函数如下所示
        @Bean
            public Docket productApi() {
                Contact contact =new Contact(
                        "Vineet Mishra",
                        "https://github.com/xbox2204",
                        "whatwillyoudo@withmyemail.com"
                );
                ApiInfo apiInfo= new ApiInfoBuilder().title("VINEET SPRING-BOOT API")
                        .description("Spring-Boot for all")
                        .termsOfServiceUrl("jUST CHILL!!!")
                        .contact(contact)
                        .licenseUrl("something@something.com").version("1.0").build();
        
                return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo)
                        .select()
                        .apis(RequestHandlerSelectors.any())
                        .build();
            }
        
        1. 最后,我从
        2. 访问了我的 swagger-ui

        http://localhost:8080/swagger-ui/index.html#

        Image of final result

        【讨论】:

          【解决方案4】:

          swagger-ui.html 页面会进行多次调用以获取所有详细信息。如果您使用浏览器的开发工具(通常只需按 F12 打开),您将在网络选项卡中看到失败的请求。您需要允许

              "/v2/api-docs",
              "/swagger-resources/**",
              "/swagger-ui.html**",
              "/webjars/**"
          

          springfox docs 中有一些信息,请查找“安全”或“授权”

          【讨论】:

          • 谢谢!我还认为这是一些安全问题。但这是一个网址问题。我试图使用 localhost:8080//swagger-ui.html 打 swagger-ui.html 但是它在 localhost:8080/swagger-ui.html 工作。
          • 你如何允许这些请求???我在哪里写这些行?在日食?哪个文件? application.properties???
          • 尝试禁用所有安全检查,stackoverflow.com/a/38405624/944593
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-10-20
          • 2010-10-17
          • 1970-01-01
          • 2011-09-19
          • 2018-06-26
          • 2020-12-13
          • 2014-07-06
          相关资源
          最近更新 更多