Springfox swagger-ui.html 未加载

Springfox swagger-ui.html not loading

我正在尝试访问 swagger ui 页面,但是当我要访问该页面时,出现此错误

此应用程序没有针对 /error 的显式映射,因此您将其视为后备

我的项目树:

招摇配置class

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket productApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.aluno"))
                .paths(regex("/api.*"))
                .build()
                .apiInfo(metaInfo());
    }

    private ApiInfo metaInfo() {

        ApiInfo apiInfo = new ApiInfo(
                "Cursos API REST",
                "API REST de registro de alunos.",
                "1.0",
                "Terms of Service",
                new Contact("Romulo Sorato", "https://www.linkedin.com/in/r%C3%B4mulo-sorato-domingos-a6524437/",
                        "romulosorato@hotmail.com"),
                "Apache License Version 2.0",
                "https://www.apache.org/licesen.html", new ArrayList<>()
        );

        return apiInfo;
    }
}

我的pom.xml

  <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>3.0.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>3.0.0</version>
    </dependency>
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>2.0.1.Final</version>
    </dependency>

为什么我无法访问 swagger-ui?

看起来 tomcat 也正在启动。 我在 pom.xml

上添加了正确的依赖项

你能试试这个吗,对我有用

@Bean
    public Docket productApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()                                       
                  .apis(RequestHandlerSelectors.basePackage("com.aluno.controller"))
                  .paths(PathSelectors.any())                   
                  .build();
    }

请检查一下,添加了这些依赖项

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

看起来你想使用 Springfox3。如果是,则尝试以下更改。

删除 springfox-swagger2 和 springfox-swagger-ui 并在 pom.xml

中添加以下依赖
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>

SwaggerConfig - 删除 @EnableSwagger2 注释

@Configuration
public class SwaggerConfig {

    @Bean
    public Docket productApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.aluno"))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(metaInfo());
    }

    private ApiInfo metaInfo() {

        ApiInfo apiInfo = new ApiInfo(
                "Cursos API REST",
                "API REST de registro de alunos.",
                "1.0",
                "Terms of Service",
                new Contact("Romulo Sorato", "https://www.linkedin.com/in/r%C3%B4mulo-sorato-domingos-a6524437/",
                        "romulosorato@hotmail.com"),
                "Apache License Version 2.0",
                "https://www.apache.org/licesen.html", new ArrayList<>()
        );

        return apiInfo;
    }
}

对于此版本,UI 的 URL 已更改如下所示:

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

如果您希望使用旧版本,那么只需使用版本2.9.2(对我来说非常好)。在您的配置文件或 UI 的 URL.

中没有其他更改需要 uired
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>