简体   繁体   中英

Swagger UI is not rendering on the browser

I have added swagger dependencies to the spring boot application and JSON is loading as expected. When I try to load UI by calling this URL http://localhost:9090/swagger-ui.html then the following error is displayed on the browser.

Whitelabel Error Page

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

There was an unexpected error (type=Not Found, status=404).

pom.xml

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

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

config class

@Configuration
@EnableSwagger2
public class SwaggerConfig {
}

Ps - My application is running under the port 9090

I found the solution. From Swagger 3.0 we don't need to add 2 dependencies into the build tool. springfox-boot-starter can be replaced instead of those 2 dependencies.

pom.xml

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

Form swagger 3.0 the URL should be http://localhost:9090/swagger-ui/ rather than http://localhost:9090/swagger-ui.html

i had similar issue. Make sure you declerate the swagger items.

   @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {


    //Swagger UI property
    registry.addResourceHandler("swagger-ui.html")
            .addResourceLocations("classpath:/META-INF/resources/");

    registry.addResourceHandler("/webjars/**")
            .addResourceLocations("classpath:/META-INF/resources/webjars/");
}

This must be declareted in the config of implementation with "WebMvcConfigurer".

More information here : https://springfox.github.io/springfox/docs/current/

If you have base url defined, for eg /rest , /api then you will need to put that as well. My config looks like this:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig extends WebMvcConfigurerAdapter{
    @Bean
    public Docket APIs() { 
        return new Docket(DocumentationType.SWAGGER_2)
          .apiInfo(metadata())
          .select()
          .apis(RequestHandlerSelectors.basePackage("<rest_controller_package>"))
          .paths(PathSelectors.any())
          .build();
    }
    
    private ApiInfo metadata() {
        return new ApiInfoBuilder()
            .title("<title_here>")
            .description("<description_here>")
            .version("BETA")
            .build();
    }
}

and pom.xml

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

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

and I access it via http://localhost:9090/api/swagger-ui.html

I think you should add into the SwaggerConfig class a Bean that return a Docket object, which specify the paths and the packages that you want to show in swagger. Something like this:

@Configuration 
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
          .select()
          .apis(RequestHandlerSelectors.basePackage(“com.spring.rest.controller”))
          .paths(PathSelectors.any())
          .build();
    }
}

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