为什么 springfox-swagger2 UI 告诉我 "Unable to infer base url."

Why does springfox-swagger2 UI tell me "Unable to infer base url."

为什么 springfox-swagger2 UI 告诉我 Unable to infer base url. 据我所知,我使用的是典型的 Swagger spring-boot 配置。

正如您在屏幕截图中看到的,支持 UI 的 swagger-fox url 是 example.com/api 。注意:当我导航到 https://localhost:9600/api/v2/api-docs/ 时,我得到一个标准的 Spring Whitelabel Error Page 。我怀疑这是问题的根源?我没有看到 Spring 没有加载 springfox-swagger2 的错误,所以我不知道为什么它不起作用。

我的配置看起来像这样(我已经尝试了这个配置的各种变体,从网上搜索建议):

@EnableSwagger2
@EnableWebMvc
@ComponentScan(basePackages = {"com.company.project"})
public class SwaggerConfig
{
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(Predicates.not(RequestHandlerSelectors.basePackage("org.springframework.boot")))
                .apis(Predicates.not(RequestHandlerSelectors.basePackage("org.springframework.cloud")))
                .apis(Predicates.not(RequestHandlerSelectors.basePackage("org.springframework.data.rest.webmvc")))
                .paths(PathSelectors.any())
                .build();
    }
}

<!-- to generate /swagger-ui.html -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.7.0</version>
</dependency>

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.7.0</version>
</dependency>

注意:有趣的是,当我尝试版本 2.6.0 时,我没有看到模式弹出窗口,但我的 Swagger UI 显示 0 api 内容。所以,我知道模态一定是相当新的?

如果这里没有足够的信息,请给我留言。

只需使用 SpringBootServletInitalizer 扩展您的主要 class。 它会工作正常。 参考如下。

public class TestApp extends SpringBootServletInitializer{

    public static void main(String[] args) {

       SpringApplication.run(TestApp.class, args);
   }
}

我能够通过添加带注释的 SpringBootApplication 来解决问题 - 正如 Mark 所建议的:

@EnableSwagger2

事实证明,我的问题的解决方案是将 @EnableSwagger2 注释与 Docket Bean 一起移动到主配置 class 中。

我在带有 Docket Bean 的子包中创建了一个单独的 class,我希望它能被 Spring 扫描并加载该 bean。也许我没有用 @Component 注释那个摘要 class,或者问题可能是别的,但我让它工作了。

对我来说,问题是 Spring 安全性不允许访问 swagger-ui 所需的某些资源。解决方案是允许匿名访问以下路径:

  • /招摇-ui.html
  • /webjars/**
  • /v2/**
  • /招摇资源/**

就我而言,无需身份验证即可访问这些资源。

对于 Spring 2 使用 @SpringBootApplication 注释并从 SpringBootServletInitializer 扩展您的 class 应用程序然后覆盖方法 SpringApplicationBuilder

@SpringBootApplication
public class TestApp extends SpringBootServletInitializer{

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(TestApp.class);
    }


    public static void main(String[] args) {
        SpringApplication.run(TestApp.class, args);
    }
}

在我的例子中,我将 springboot(2.1.2)swagger2(2.9.2) 应用程序部署为 war文件在tomcat8.5中得到同样的错误。

后来我可以通过扩展这个 class 文件来修复 AppConfig extends SpringBootServletInitializer

我完整的 AppConfig 文件

package org.mydomain

import java.net.URL;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.scheduling.annotation.EnableScheduling;

import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication(scanBasePackages = "org.mydomain")
@EntityScan({"org.mydomain.entity"})
@EnableJpaRepositories({"org.mydomain.repository"})
@EnableSwagger2
@EnableScheduling
public class AppConfig extends SpringBootServletInitializer {

  // We can optionally have spring boot way of starting main method
  public static void main(String[] args) {
     SpringApplication.run(AppConfig.class, args);
  }
}

This is the blog I referred how to deploy springboot application into tomcat

注意:我没有配置 swagger docket 只是使用默认实现。

我正在使用 spring 引导。

在我的例子中,我在包名上弄错了。 在我的项目中,基本包名称是

org.liferayasif.documents

所以我的配置文件应该在里面

org.liferayasif.documents.config

config -> 你可以给任何名字。

但我错误地在下面输入了不同的包名

org.liferayasif.config

在我的案例中,一旦我将配置文件放入正确的包名称中

org.liferayasif.documents.config

然后它就正常工作了。

我需要在 Spring 安全方法中添加对资源的匿名访问: 受保护的无效配置(HttpSecurity http)

.antMatchers("/api/**", "/swagger-ui.html", "/webjars/**", "/v2/**", "/swagger-resources/**").anonymous()

然后它开始工作。

就我而言,我的 WebSecurityConfig.java:

中有这一行
.antMatchers("swagger-ui.html").permitAll()

当我添加这个时:

.antMatchers("/swagger-resources/**").permitAll()

我的问题已解决。

OAHH,对我来说这是新事物。 springfox-swagger2springfox-swagger-ui

我有 2 个不同的版本

之前:

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        **<version>2.6.1</version>**
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        **<version>2.9.2</version>**
    </dependency>

之后:

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        **<version>2.9.2</version>**
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        **<version>2.9.2</version>**
    </dependency>

这解决了我的问题。

我有同样的问题,但(也许)我在 pom.xml

中有一个版本不匹配

我的项目版本是:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.0.M5</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

然后,当我添加以下依赖项时它开始工作:

  <dependency>
        <groupId>org.springframework.plugin</groupId>
        <artifactId>spring-plugin-core</artifactId>
        <version>1.2.0.RELEASE</version>
    </dependency>

发现于: https://github.com/springfox/springfox/issues/2932

在Springboot应用的主文件中添加@EnableSwagger2注解

@SpringBootApplication
@EnableSwagger2
public class SampleApplication {

    public static void main(String[] args) {
        SpringApplication.run(SampleApplication.class, args);
    }

}

先检查你的Java版本,它似乎不适用于版本Java 11,尝试使用Java 8似乎没问题

这对我有用。

@Override
    public void configure(WebSecurity web) throws Exception {
         web
        .ignoring()
        .antMatchers(
                "/swagger-resources/**",
                "/swagger-ui.html",
                "/v2/api-docs",
                "/webjars/**");
    }

确定根路径。