I get 404, when trying to open swagger-ui.html

Viewed 1211

I haven't Spring Boot, only Spring MVC and Security. My config:

@Configuration
@EnableTransactionManagement
@EnableWebMvc
@EnableSwagger2
@PropertySource("classpath:access.properties")
@ComponentScan(basePackages = {"com.company.service", "com.company.dao", "com.company.controller"})

public class SpringConfig extends WebMvcConfigurationSupport {

@Value("${url}")
private String URL;
@Value("${user}")
private String USER;
@Value("${password}")
private String PASSWORD;

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

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

........ 

pom.xml

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

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

On http://localhost:8080/api/v2/api-docs i get enormous JSON, but whe trying to get ui on http://localhost:8080/api/swagger-ui.html i get tomcat 404 not found

Tomcat Log

0:0:0:0:0:0:0:1 - - [30/Jun/2020:11:50:09 +0300] "GET /api/v2/api-docs HTTP/1.1" 200 20832
0:0:0:0:0:0:0:1 - - [30/Jun/2020:11:51:51 +0300] "GET /api/swagger-ui.html HTTP/1.1" 404 702
0:0:0:0:0:0:0:1 - - [30/Jun/2020:11:51:54 +0300] "GET /api/swagger-ui.html HTTP/1.1" 404 702

Many articles talk about possible solutions to the problem using the Spring Boot or adding annotation @EnableSwagger2, but it's useless in my case.

3 Answers

Try this:

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

Or remove /api/ when you want to access to your docs

I think you need to add the package where your controller is so it will be scanned by swagger : .apis(RequestHandlerSelectors.basePackage("your package"))

Here's an example

@Configuration
@EnableSwagger2
public class SwaggerConfiguration {

    @Bean
     public Docket getDocketInstance() {
         return new Docket(DocumentationType.SWAGGER_2) 
                 .apiInfo(new ApiInfoBuilder()
                            .title("Spring Boot project")
                            .description("Spring Boot bootstrap project")
                            .version("0.1")
                            .license("Unlicense")
                            .build())
                  .select()  
                  .apis(RequestHandlerSelectors.basePackage("your package"))             
                  .paths(PathSelectors.any())                          
                  .build();  
     }
<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-ui</artifactId>
    <version>1.6.6</version>
</dependency>

FOR SPRING BOOT VERSION > 2.5. Use this dependency for swaggger. It worked for me

Related