简体   繁体   中英

Swagger ui returns Whitelabel Error

I used swagger 2.9.2 in my spring boot app.

localhost:8080/api-docs works fine.

However, localhost:8080/swagger-ui.html returns writelabel error .

localhost:8080/v2/swagger-ui.html and localhost:8080/api/swagger-ui.html return the same error. I must have missed something simple. Thanks.

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

In build.gradle , I have dependency of 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        
    }

Hey I am using Spring boot 2.1.4, Swagger 2.9.2, I faced the same issue and got resolved by the following:

  • It seems that you have the required dependencies so this is not the issue.
  • I think the issue that you have to implement WebMvcConfigure and override addResourceHandlers method:

     @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/"); }

Just try to add it and Share what happen with you.

Return the Docket bean like below :

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

and add @RestController annotation above your controller class

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

I have the same problem, and solve with this Docket bean config:

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

it works for me.

I faced the same issue and got resolved by the following

You can use one dependency:

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>
  • SwaggerConfig class like below :

     @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/"); } }
  • do not use @EnableSwagger2 3.0 version

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM