【问题标题】:Swagger with Spring Boot 2.0 leads to 404 error page使用 Spring Boot 2.0 的 Swagger 导致 404 错误页面
【发布时间】:2018-11-10 02:46:33
【问题描述】:

我正在尝试将我的 Spring Boot 版本 2.0.1.RELEASESwagger 集成。

blog post 看来,只需添加两个 Maven 依赖项就很容易,一切都应该正常工作。

所以我在 pom 中添加了以下依赖项:

        <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.8.0</version>
    </dependency>

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

并创建了SwaggerConfig bean:

@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
    Docket docket = new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.any())
            .paths(PathSelectors.any())
            .build();

    return docket;
   }
}

在属性文件中,我在尝试使其工作时得到了这 3 个条目:

spring.application.name=cat-service
management.server.servlet.context-path=/cat-service
server.servlet.contextPath=/cat-service

但最后,当访问时

http://localhost:8080/cat-service/api/v2/api-docs

处的 UI 页面

http://localhost:8080/cat-service/swagger-ui.html

我收到 page not found 错误。

我找到了this issues in the swagger github pagethis question in stackoverflow,但我无法更改我的404 错误。

【问题讨论】:

标签: java spring-boot swagger


【解决方案1】:

我能够使其与 Spring boot 版本 2.0.4.RELEASEthis blog post 一起工作:

我添加了这些依赖项:

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

还有这个配置文件:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SpringFoxConfig {
    @Bean
    public Docket apiDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }
}

它奏效了。

可以通过 /swagger-ui.html# 访问 Swagger 用户界面

【讨论】:

    【解决方案2】:

    请查看参考:https://springfox.github.io/springfox/docs/current/

    “2.1.3. 从现有的 2.x 版本迁移”

    您可以从 pom.xml 中删除 springfox-swagger2 和 springfox-swagger-ui 并添加 springfox-boot-starter(例如版本 3.0.0)。您也可以删除 @EnableSwagger2 注释

    并且:“swagger-ui 位置已从 http://host/context-path/swagger-ui.html 移动到 http://host/context-path/swagger-ui/index.html 或 http:// /host/context-path/swagger-ui/ 简称。这使得它可以更好地作为 web jar 拉取并在不需要时使用配置属性将其关闭。"

    【讨论】:

    • ui页面的路径使它工作 - swagger-ui/index.html
    【解决方案3】:

    首先在你的 springboot 文件的同一个包中添加 SwaggerConfig.java 文件,如下例所示。

    @Configuration
    @EnableSwagger2
    @EnableWebMvc
    public class SwaggerConfig extends WebMvcConfigurerAdapter {                                    
        @Bean
        public Docket api() { 
            return new Docket(DocumentationType.SWAGGER_2)  
              .select()                                  
              .apis(RequestHandlerSelectors.any())              
              .paths(PathSelectors.any())                          
              .build();                                           
        }
    
       @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/");
       }
    
    }
    

    试试这个 http://localhost:8080/spring-security-rest/api/swagger-ui.html 要么 http://localhost:8080/spring-security-rest/swagger-ui.html

    如果这不起作用,请尝试更改 application.properties 中的路径

    将此添加到 application.properties:

    server.servlet-path=/loop-service
    

    并尝试以下网址:

    http://localhost:8080/loop-service/swagger-ui.html(用户界面文档)

    http://localhost:8080/loop-service/v2/api-docs(JSON 文档)

    结果:

    【讨论】:

      【解决方案4】:

      我也遇到了同样的问题(springfox 3.0.0 找不到 404)。通过将日志记录级别设置为“DEBUG”,我能够看到/v3/api-docs 的端点并且它们工作正常,但没有关于“swagger-ui”的任何内容。

      终于找到https://github.com/springfox/springfox/issues/3285,表示:

      3.0.0 中的新 url 是 /swagger-ui/index.html 或 /swagger-ui/ 而不是 /swagger-ui.html"

      难道他们没有添加调试日志来指示 Swagger UI 的可用位置吗?

      【讨论】:

        【解决方案5】:

        只需使用 springdoc-openapi-ui 代替。

        依赖:

        <dependency>
             <groupId>org.springdoc</groupId>
             <artifactId>springdoc-openapi-ui</artifactId>
             <version>1.5.9</version>
        </dependency>
        

        然后对于 UI,只需转到:

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

        对于 Json,请转到:

        • http://localhost:8080/v3/api-docs

        对于 yaml:

        • http://localhost:8080/api-docs.yaml

        这就是它的全部内容......不需要注释/配置。干杯!

        如果您使用 spring security,请确保您可以访问以下路径以使其正常工作:

        • /swagger-ui.html
        • /swagger-ui/**
        • /v3/api-docs/**

        更多信息: https://www.baeldung.com/spring-rest-openapi-documentation

        【讨论】:

        • 截至 2021 年 9 月,这应该是公认的答案 :)
        【解决方案6】:

        这对我有用,我使用了 WebMvcConfigurer 而不是 WebMvcConfigurerAdapter,因为该类已被弃用。

        @Configuration
        @EnableSwagger2
        public class SwaggerConfig implements WebMvcConfigurer {
          @Bean
          public Docket productApi() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .select()                
                    .apis(RequestHandlerSelectors.basePackage("com.illary.controller"))
                    .paths(PathSelectors.any())
                    .build()            
                    .apiInfo(metaData());
          }
          private ApiInfo metaData() {
            return new ApiInfoBuilder()
                    .title("Spring Boot Swagger App")
                    .description("\"Spring Boot Swagger Server App\"")
                    .version("1.0.0")
                    .license("Apache License Version 2.0")
                    .licenseUrl("https://www.apache.org/licenses/LICENSE-2.0\"")
                    .build();
          }
        
          public ApiInfo apiInfo() {
            final ApiInfoBuilder builder = new ApiInfoBuilder();
            builder.title("Swagger Test App").version("1.0").license("(C) Copyright Test")
            .description("The API provides a platform to query build test swagger api");
        
            return builder.build();
          }
        
          @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/");
          }
        }
        

        【讨论】:

          【解决方案7】:

          另一种可能性是 Swagger 配置文件的位置;您需要将其放在 spring boot 文件的同一个包或子包中。 如上图:

          【讨论】:

            【解决方案8】:

            如果您将 Spring Boot 升级到 2+,请不要忘记将 server.contextPath 更改为 server.servlet.contextPath

            【讨论】:

              【解决方案9】:

              对于 Spring Boot,只需使用下面的依赖项,它就可以在 URL /swagger-ui/ 上工作(尾部斜杠是强制性的)。

              <dependency>
                  <groupId>io.springfox</groupId>
                  <artifactId>springfox-boot-starter</artifactId>
                  <version>3.0.0</version>
              </dependency>
              

              在尝试之前,我尝试使用 swagger2swagger-ui 的经典依赖项,但建议的 URL 均无效。

              【讨论】:

              • 来吧 :D 一个斜杠是我的问题 :) 谢谢,但为什么它甚至是必要的??
              【解决方案10】:

              Springfox swagger UI 在路径 /swagger-ui/index.html 处打开。

              确保您允许正确的 ResourceLocations从拦截器中排除的路径

                  @Override
                  public void addResourceHandlers(ResourceHandlerRegistry registry) {
                      registry.addResourceHandler("/swagger-ui/**").addResourceLocations("classpath:/META-INF/resources/swagger-ui.html");
                      registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
                  }
              

              注意:我使用的是 Springfox swagger 3.0.0 版和 Springboot 2.5.3 版

              【讨论】:

                【解决方案11】:

                解决方案:您只需从配置类中删除 @EnableWebMvc

                说明: @EnableWebMvc开启类org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport。在spring-boot中,有一个自动配置类org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration,有注解@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)

                我们最终得到的结果:通过将 @EnableWebMvc 添加到项目中,我们自己负责一切,因为我们关闭了 spring-boot 自动配置。

                【讨论】:

                • 删除它会导致 Error creating bean with name webMvcRequestHandlerProvider 等等...
                【解决方案12】:

                application.properties 中添加以下行后,它开始工作

                spring.web.resources.static-locations: classpath:/webapp/
                

                但我不确定我们为什么要添加这个。 添加我认为可能相关的代码。依赖如下:

                    <parent>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-parent</artifactId>
                        <version>2.4.1</version>
                        <relativePath/> <!-- lookup parent from repository -->
                    </parent>
                    ...
                    <dependencies>
                        <dependency>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-starter</artifactId>
                        </dependency>
                        <dependency>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-starter-web</artifactId>
                        </dependency>
                        <dependency>
                            <groupId>javax.validation</groupId>
                            <artifactId>validation-api</artifactId>
                            <version>2.0.1.Final</version>
                        </dependency>
                
                        <!-- Swagger Dependencies -->
                        <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>
                
                        <dependency>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-starter-test</artifactId>
                            <scope>test</scope>
                        </dependency>
                    </dependencies>
                

                主类为

                import org.springframework.boot.SpringApplication;
                import org.springframework.boot.autoconfigure.SpringBootApplication;
                import org.springframework.context.annotation.Bean;
                import springfox.documentation.builders.PathSelectors;
                import springfox.documentation.builders.RequestHandlerSelectors;
                import springfox.documentation.spi.DocumentationType;
                import springfox.documentation.spring.web.plugins.Docket;
                import springfox.documentation.swagger2.annotations.EnableSwagger2;
                
                @SpringBootApplication
                @EnableSwagger2
                public class ProxyApplication {
                
                    public static void main(String[] args) {
                        SpringApplication.run(ProxyApplication.class, args);
                    }
                
                    @Bean
                    public Docket api() {
                        return new Docket(DocumentationType.SWAGGER_2)
                                .select()
                                .apis(RequestHandlerSelectors.basePackage("com.ghsatpute.proxy"))
                                .paths(PathSelectors.any())
                                .build();
                    }
                }
                

                【讨论】:

                  【解决方案13】:

                  如果它可以帮助某人,请添加它。这个网址对我有用

                  http://localhost:8003/v2/api-docs
                  

                  对于 swagger-ui,这个网址开箱即用:

                  http://localhost:8003/swagger-ui.html 
                  

                  检查您是否使用正确配置的 url 来进行 swagger 规范。像http://localhost:8080/spring-security-rest/api/swagger-ui/这样的网址

                  没用,我得到了 404。

                  【讨论】:

                    【解决方案14】:

                    以下是在 Spring Boot 应用中启用 Swagger 的步骤:

                    1. 添加 springfox-swagger2springfox-swagger-ui 依赖项。
                    2. 要在您的项目中启用 Swagger2,您应该使用 @EnableSwagger2
                    3. 在下面定义一个 docket bean 广告
                    
                            @Bean
                            public Docket docket(){
                                    return new Docket(DocumentationType.SWAGGER_2).groupName("group-name").apiInfo(apiInfo()).select().paths(predicate()).build();
                            }
                    
                    
                            private Predicate<String> predicate() {
                                    return or(regex("/api/v1.*"), regex("/api/v2.*"));
                            }
                        
                            private ApiInfo apiInfo() {
                                return new ApiInfoBuilder().title("The title of the your choice")
                                        .contact("your@email.com").license("Licence name ").version("1.0").build();
                            }
                        
                    

                    【讨论】:

                      【解决方案15】:

                      Swagger UI url 模式: localhost:&lt;server.port&gt;/&lt;server.servlet.context-path&gt;/swagger-ui.html

                      【讨论】:

                        【解决方案16】:

                        删除 @EnableWebMvc 后它对我有用

                        【讨论】:

                        • 删除它会导致 Error creating bean with name webMvcRequestHandlerProvider 等等...
                        【解决方案17】:

                        面临同样的问题,只是通过改变依赖关系来解决 参考https://www.vojtechruzicka.com/documenting-spring-boot-rest-api-swagger-springfox/

                        编译“io.springfox:springfox-swagger2:2.9.2”

                        以前使用过 - 不工作

                        编译组:'io.springfox',名称:'springfox-swagger2',版本:'3.0.0'

                        【讨论】:

                          【解决方案18】:

                          我被这个问题困扰了一天。我的问题是端口配置不正确。检查您的application.yml/application.properties 文件以了解端口配置。在那里我错误地添加了两者 server.port , management.server.port 具有不同的值?。

                          【讨论】:

                            猜你喜欢
                            • 2017-10-06
                            • 2019-04-02
                            • 2018-07-22
                            • 2014-11-30
                            • 1970-01-01
                            • 2017-08-07
                            • 1970-01-01
                            • 1970-01-01
                            相关资源
                            最近更新 更多