Swagger UI返回Whitelabel错误

时间:2018-08-22 15:08:40

标签: java swagger swagger-ui

我在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        
    }

5 个答案:

答案 0 :(得分:0)

返回Docket bean如下:

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

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

答案 1 :(得分:0)

嘿,我使用的是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 :(得分:0)

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

答案 3 :(得分:0)

我有同样的问题,并使用以下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());
}

对我有用。

答案 4 :(得分:0)

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

您可以使用一个依赖项:

<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