【问题标题】:Swagger ui returns Whitelabel ErrorSwagger ui 返回 Whitelabel 错误
【发布时间】:2018-08-22 15:08:40
【问题描述】:

我在我的 Spring Boot 应用中使用了 swagger 2.9.2。

localhost:8080/api-docs 工作正常。

但是,localhost:8080/swagger-ui.html 返回writelabel error

localhost:8080/v2/swagger-ui.htmllocalhost:8080/api/swagger-ui.html 返回相同的错误。 我一定错过了一些简单的事情。谢谢。

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Wed Aug 22 10:05:48 CDT 2018
There was an unexpected error (type=Not Found, status=404).
No message available

build.gradle,我有springfox的依赖。

compile("io.springfox:springfox-swagger2:2.9.2")
compile("io.springfox:springfox-swagger-ui:2.9.2")

swaggerconfig.java 

@Configuration
@EnableSwagger2
public class SwaggerConfig{
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage(MyServiceController.class.getPackage().getName()))
                //.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.ant("/api/**"))
                .build()
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        String description = "Company -  My API";
        return new ApiInfoBuilder()
                .title("REST API")
                .description(description)
                .version("1.0")
                .build();
    }

MyServiceController.java 

 @ApiOperation(value = "some description",
            response = MyServiceResponse.class)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "ok"),
            @ApiResponse(code = 400, message = "Bad Request"),
            @ApiResponse(code = 401, message = "not authorized"),
            @ApiResponse(code = 403, message = "not authenticated"),
            @ApiResponse(code = 404, message = "The resource you were trying to reach is not found"),
            @ApiResponse(code=500, message = "Interval Server Error")
    })
    @RequestMapping(method = RequestMethod.POST, value = "/api/component/operation", consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
    @ResponseBody
    {
        do something        
    }

【问题讨论】:

  • 你能试试PathSelectors.any()而不是PathSelectors.ant("/api/**")看看它是否改变了什么?
  • @Arnaud 试过了,没用。同样的错误
  • @toosensitive 你找到解决方案了吗?

标签: java swagger swagger-ui


【解决方案1】:

嘿,我使用的是 Spring boot 2.1.4,Swagger 2.9.2,我遇到了同样的问题,并通过以下方式解决:

  • 您似乎拥有所需的依赖项,所以这不是问题。
  • 我认为你必须实现 WebMvcConfigure 并覆盖 addResourceHandlers 方法的问题:

    @Configuration
    @EnableSwagger2
    public class SwaggerConfig implements WebMvcConfigurer {
     // your Docket and ApiInfo methods
    
     @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/");
     }
    

只需尝试添加它并与您分享发生的事情。

【讨论】:

    【解决方案2】:

    返回 Docket bean,如下所示:

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo());
    }
    

    并在控制器类上方添加@RestController 注释

    【讨论】:

    • @toosensitive 您能否分享您的代码的 GitHub 链接以便我检查?
    【解决方案3】:
    So if u have correctly written the swaggerConfig code also added the right dependencies and still getting error
    The ultimate solution is 
    You need to have perfect combination of swagger version and spring boot version
    
    Just change the spring boot and swagger version as below
    Check in your pom.xml or gradle build
    Spring boot version :- <version>1.4.1.RELEASE</version>
    Swagger and Sawgger ur version:- <version>2.2.2</version>
    
    There are other combinations available but that u need to try on trial basis
    

    【讨论】:

      【解决方案4】:

      我有同样的问题,用这个 Docket bean 配置解决:

      @Bean
      public Docket api() {
          return new Docket(DocumentationType.SWAGGER_2)
                  .select()
                  .apis(RequestHandlerSelectors.any())
                  .paths(Predicates.not(PathSelectors.regex("/error.*")))
                  .build()
                  .apiInfo(this.apiInfo());
      }
      

      它对我有用。

      【讨论】:

        【解决方案5】:

        我遇到了同样的问题并通过以下方式解决了

        你可以使用一个依赖:

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>
        
        • SwaggerConfig 类如下:

            @Configuration
            @EnableSwagger2
            public class SwaggerConfig implements WebMvcConfigurer {
            @Bean
            public Docket api(){
            return new Docket(DocumentationType.SWAGGER_2);
            }
            @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/");
           }
          }
          
        • 不要使用@EnableSwagger2 3.0版本

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-02-16
          • 2023-02-09
          • 2018-07-27
          • 1970-01-01
          • 2019-11-08
          • 2018-06-11
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多