【问题标题】:Receiving 404 error on Swagger UI with Spring使用 Spring 在 Swagger UI 上接收 404 错误
【发布时间】:2018-06-12 09:58:58
【问题描述】:

我正在将 Swagger UI 与 Spring Boot 应用程序集成。当我点击 swagger-ui.html 时。我收到 404 错误。我的配置类如下:

@Configuration
@EnableSwagger2
//@Import(SwaggerConfiguration.class)
public class SwaggerConfig  {
    @Bean
        public Docket api() {
            return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
        }
...

错误信息:

{"status":404,"message":"HTTP 404 Not Found","link":"https://jersey.java.net/apidocs/2.8/jersey/javax/ws/rs/NotFoundException.html"}

【问题讨论】:

标签: java spring-boot swagger swagger-ui


【解决方案1】:

我遇到了类似的问题,其中 /swagger-ui.html(或新的端点版本 /swagger-ui/)返回 404,但 /v2/api-docs 返回了有效的 json。解决方案是从 3.0.0 降级 swagger 版本。到 2.9.2。

【讨论】:

    【解决方案2】:

    希望这会有所帮助,请在下面找到我的工作 swagger 配置:

    @Configuration
    @EnableSwagger2
    @Profile({"!production"})
    public class SwaggerConfiguration extends WebMvcConfigurerAdapter {
    
        @Autowired
        private ServletContext servletContext;
    
    
        @Bean
        public Docket api() {
    
            return new Docket(DocumentationType.SWAGGER_2)
                    .host("localhost")
                    .directModelSubstitute(LocalDate.class, Date.class)
                    .pathProvider(new RelativePathProvider(servletContext) {
                        @Override
                        public String getApplicationBasePath() {
                            return "/";
                        }
                    })
                    .select()
                    .apis(RequestHandlerSelectors.any())
                    .paths(PathSelectors.any())
                    .build();
        }
    }
    

    【讨论】:

      【解决方案3】:

      您的某个配置类中有@EnableWebMvc 吗?如果是这样,您将不得不手动进行资源配置,如下所示:

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

      【讨论】:

        【解决方案4】:

        如果您使用的是版本 > 3,请使用 /swagger-ui/ 而不是 swagger-ui.html

        【讨论】:

          【解决方案5】:

          请在 pom.xml 中引入以下依赖

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

          【讨论】:

            【解决方案6】:

            我遇到了同样的问题,通过用 springfox-boot-starter:3.0.0 替换 springfox-swagger2 和 springfox-swagger-ui 依赖项并删除 @EnableSwagger2 注释来修复它。请注意,Swagger url 也从 http://localhost:8080/swagger-ui.html 更改为 http://localhost:8080/swagger-ui/index.html。

            【讨论】:

              猜你喜欢
              • 2020-04-13
              • 2018-06-11
              • 1970-01-01
              • 2020-10-23
              • 1970-01-01
              • 2021-05-18
              • 2019-11-24
              • 2016-02-01
              • 2018-11-10
              相关资源
              最近更新 更多