关于swagger ui:为什么springfox-swagger2 UI告诉我“无法推断基本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网址为example.com/api。 注意:导航至:https:// localhost:9600 / api / v2 / api-docs /时,我得到标准的Spring Whitelabel Error Page。 我怀疑这是问题的根源吗? 我没有看到Spring未加载springfox-swagger2的错误,所以我不知道为什么它不起作用。

enter image description here

我的配置看起来像这样(并且我已经尝试过从网上搜索建议的各种配置变化):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@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();
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
<!-- to generate /swagger-ui.html -->
<dependency>
    <groupId>io.springfox</groupId>
    springfox-swagger2</artifactId>
    <version>2.7.0</version>
</dependency>

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

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

如果此处没有足够的信息,请给我评论。


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

1
@EnableSwagger2


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

  • /swagger-ui.html
  • / webjars / **
  • / v2 / **
  • / swagger-resources / **

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


事实证明,解决我的问题的方法是将@EnableSwagger2注释与Docket Bean一起移入主Configuration类。

我已经在Docket Bean的子包中创建了一个单独的类,我期望它会被Spring扫描并加载Bean。也许我无法用@Component注释该档案夹类,或者问题可能出在其他方面,但是我让它起作用了。


只需使用SpringBootServletInitalizer扩展您的主类即可。
它将正常工作。
以波纹管为参考。

1
2
3
4
5
6
7
public class TestApp extends SpringBootServletInitializer{

    public static void main(String[] args) {

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


就我而言,我的WebSecurityConfig.java中包含以下行:

1
.antMatchers("swagger-ui.html").permitAll()

当我添加这个:

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

我的问题解决了。


我正在使用弹簧靴。

就我而言,我在包裹名称中输入了错误。
在我的项目中,基本包名称是

1
org.liferayasif.documents

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

1
org.liferayasif.documents.config

配置->你可以给任何名字。

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

1
org.liferayasif.config

一旦我将配置文件放入适当的软件包名称中

1
org.liferayasif.documents.config

然后它正常工作。


对于Spring 2,使用@SpringBootApplication批注并从SpringBootServletInitializer扩展您的类应用程序,然后重写SpringApplicationBuilder方法

1
2
3
4
5
6
7
8
9
10
11
12
13
@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);
    }
}

首先检查您的Java版本,它似乎不能与版本Java 11一起使用,尝试使用Java 8似乎很好


在Springboot应用程序的主文件中添加@EnableSwagger2批注

1
2
3
4
5
6
7
8
9
@SpringBootApplication
@EnableSwagger2
public class SampleApplication {

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

}

我遇到了同样的问题,但是(也许)在pom.xml中版本不匹配

我的项目版本是:

1
2
3
4
5
6
<parent>
    <groupId>org.springframework.boot</groupId>
    spring-boot-starter-parent</artifactId>
    <version>2.2.0.M5</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

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

1
2
3
4
5
  <dependency>
        <groupId>org.springframework.plugin</groupId>
        spring-plugin-core</artifactId>
        <version>1.2.0.RELEASE</version>
    </dependency>

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


OAHH,对我来说,这是新事物。我为springfox-swagger2springfox-swagger-ui有2个不同的版本

之前:

1
2
3
4
5
6
7
8
9
10
11
    <dependency>
        <groupId>io.springfox</groupId>
        springfox-swagger2</artifactId>
        **<version>2.6.1</version>**
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        springfox-swagger-ui</artifactId>
        **<version>2.9.2</version>**
    </dependency>

后:

1
2
3
4
5
6
7
8
9
10
11
    <dependency>
        <groupId>io.springfox</groupId>
        springfox-swagger2</artifactId>
        **<version>2.9.2</version>**
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        springfox-swagger-ui</artifactId>
        **<version>2.9.2</version>**
    </dependency>

这解决了我的问题。


我需要在Spring Security方法中添加对资源的匿名访问:
保护无效的configure(HttpSecurity http)

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

然后它开始工作。


就我而言,我正在将带有swagger2(2.9.2)应用程序的springboot(2.1.2)部署为tomcat8.5中的war文件,并得到了相同的错误。

后来我仅通过扩展此类文件AppConfig extends SpringBootServletInitializer就能够修复

我完整的AppConfig文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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);
  }
}

这是我提到的如何将springboot应用程序部署到tomcat中的博客

注意:我还没有使用默认实现来配置。