springfox: 404 when use swagger2-ui with spring-boot-1.5.6

Please take the time to search the repository, if your question has already been asked or answered.

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

Get localhost:8080/v2/api-docs is ok, but localhost:8080/swagger-ui.html returns 404 . Spring-boot version is 1.5.6.RELEASE

Steps to Reproduce

  1. Clone https://github.com/ihappyk/Springboot-practice.git
  2. Change the version of Swagger Jars to 2.6.1 in Pom.xml
  3. Start application via mvn spring-boot:run
  4. Open URL http://localhost:8080/api-docs
  5. Open URL http://localhost:8080/swagger-ui.html

Similar to the issue #1747

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 1
  • Comments: 67 (20 by maintainers)

Most upvoted comments

@ihappyk config class


@Configuration
@EnableWebMvc
public class WebConfiguration extends WebMvcConfigurerAdapter {
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(new MappingJackson2HttpMessageConverter(
                new Jackson2ObjectMapperBuilder()
                        .propertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE)
                        .dateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))
                        .build()));
    }


    @Bean
    public AccessTokenVerifyInterceptor tokenVerifyInterceptor() {
        return new AccessTokenVerifyInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(tokenVerifyInterceptor()).addPathPatterns("/m/*");
        super.addInterceptors(registry);
    }

    @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


        <!-- api doc start -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${swagger.version}</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>${swagger.version}</version>
        </dependency>
        <!-- api doc end -->

swagger class config , put it spring run class same directory


@Configuration
@EnableSwagger2
public class Swagger {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.grils.web"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("接口文档")
                .description("swagger 自动生成文档")
                .termsOfServiceUrl("http://localhost:8080/")
                .contact("xiaoshuo")
                .version("1.0")
                .build();
    }
}

i use spring-boot version 1.5.6.RELEASE,swagger2 2.7.0,has same problem,need add some config in WebConfiguration


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

it’s working

Remove this dependencies and update versions for both dependencies to 2.7.0. If you try localhost:8080/swagger-resources/{files} you will see work good, probably. 😃

I had the same problem. I was following this tutorial here http://www.baeldung.com/swagger-2-documentation-for-spring-rest-api.

I had a default @RequestMapping from a leftover test that was mapped to the root of the application.

   @RequestMapping(method = RequestMethod.GET)
    public Greeting greeting() {
        return new Greeting("Greetings from Planet Earth!");
    }

I removed that (no default @RequestMapping), ran again and everything was working fine. LESSON: no default root mapping with Swagger UI.

Use Spring Boot add the following code in the Application file can fix.

  @Bean
  public WebMvcConfigurerAdapter adapter() {
    return new WebMvcConfigurerAdapter() {
      @Override
      public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
            .addResourceLocations("classpath:/META-INF/resources/swagger-ui.html");
        registry.addResourceHandler("/webjars/**")
            .addResourceLocations("classpath:/META-INF/resources/webjars/");

        super.addResourceHandlers(registry);
      }
    };
  }

I use spring boot version 1.5.10.RELEASE,springfox version 2.8.0.

I removed @EnableWebMvc annotation and /swagger-ui.html worked like a charm.

I had the same problem on my own test project today. The root cause of my problem is that there is already a handler mapped at the base URL. (This handler seem to take precedence over the handler of swagger-ui). As a result, the swagger ui is hidden

The solution is simple: I remapped that handler at a different URL and voila, the swagger-ui.html is there!

Relavent dependency (Gradle): compile(“org.springframework.boot:spring-boot-starter-web:1.5.11.BUILD-SNAPSHOT”) compile (“io.springfox:springfox-swagger2:2.7.0”) compile (“io.springfox:springfox-swagger-ui:2.7.0”)

Clear maven repository cache and reinstall all dependencies again, issue disappeared.

By the way I change editor from eclipse java to eclipse j2ee. I don’t know if this would cause the issue.

I had the same problem: 404 on GET swagger-ui.html, spring boot 1.5.9 application, springfox-swagger2 and springfox-swagger-ui 2.7.0 dependencies. After trying many different things suggested by different people with no success, I finally found the problem. I had a class in a shared lib with @ExceptionHandler methods that contained the @EnableWebMvc annotation. When I removed @EnableWebMvc swagger-ui.html worked. :^) EDIT: The swagger-ui.html page also works with springfox 2.8.0.

@dilipkrish Yes, in fact that was the issue in my case. Thank you 😃

Same issue to @denis111 @DemianTinkiel

swagger2 = 2.7.0 swagger-ui = 2.7.0 springBootVersion = ‘1.5.9.RELEASE’

All possible solutions are not worked.

pom.xml
        <parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.9.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<spring-cloud.version>Edgware.RELEASE</spring-cloud.version>
	</properties>

	<dependencies>	
   	
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		
		<dependency>
            <groupId>org.xerial</groupId>
            <artifactId>sqlite-jdbc</artifactId>
        </dependency>
		<dependency>
    		<groupId>com.zsoltfabok</groupId>
    		<artifactId>sqlite-dialect</artifactId>
    		<version>1.0</version>
		</dependency>
		
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		
		<dependency>
    		<groupId>io.springfox</groupId>
    		<artifactId>springfox-swagger2</artifactId>
    		<version>2.7.0</version>
		</dependency>
		<dependency>
    		<groupId>io.springfox</groupId>
    		<artifactId>springfox-swagger-ui</artifactId>
    		<version>2.7.0</version>
		</dependency>
		
		<dependency>
    		<groupId>org.modelmapper</groupId>
    		<artifactId>modelmapper</artifactId>
    		<version>1.1.2</version>
		</dependency>
	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>${spring-cloud.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

application.yml

 spring:
  datasource:
    url: jdbc:sqlite:XXX
    driverClassName: org.sqlite.JDBC
  jpa:
    show-sql: true
    properties:
      hibernate:
        dialect: org.hibernate.dialect.SQLiteDialect
        ddl-auto: validate
    hibernate:
      naming.implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl
      naming.physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
  jackson:
    date-format: yyyy-MM-dd'T'HH:mm:ssXXX 
    time-zone: GMT+8
    
server:
  port: 40112

Operating System: Windows Browser: Edge, Chrome, Firefox

@adiIspas i am still facing the same issue. 👎

I created a sample application that uses Spring 5 Reactive WebFlux and Springfox 3.0.0-SNAPSHOT that gets around the 404 issue for swagger-ui.html https://github.com/mpanicker/reactive-spring-swagger

Not sure why it his the case, but I also needed to add a web mvc configurer. I do use a requestmapping at the root of my controller to add /api everywhere. Anyway there is the kotlin + spring 5 version:

@Configuration
@EnableSwagger2
class SwaggerConfig {

    @Bean
    fun api(): Docket = Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.github"))
            .build()

    @Bean
    fun adapter(): WebMvcConfigurer = object : WebMvcConfigurer {
        override fun addResourceHandlers(registry: ResourceHandlerRegistry) {
            registry.addResourceHandler("swagger-ui.html")
                    .addResourceLocations("classpath:/META-INF/resources/swagger-ui.html")
            registry.addResourceHandler("/webjars/**")
                    .addResourceLocations("classpath:/META-INF/resources/webjars/")

            super.addResourceHandlers(registry)
        }
    }
}

It’s got resolve I have to configure changes in jersey file with annotation @Application Path

On 30-Mar-2018 8:56 PM, “Dilip Krishnan” notifications@github.com wrote:

What issue are you facing @aditi-goel https://github.com/aditi-goel? Are you seeing a 404 error? Yes different modules should work.

Also take a look at the demos https://github.com/springfox/springfox-demos project.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/springfox/springfox/issues/2037#issuecomment-377549368, or mute the thread https://github.com/notifications/unsubscribe-auth/AYmDcMWeBVZyc2eh69PGhGhuUYVD_eeFks5tjk60gaJpZM4PX4K8 .

@oaoit solution worked for me after 2 days of hustling. Many thanks!

I had the same setup as silentsnooc above, spring boot 1.5.9, swagger2 and swagger-ui 2.7.0. It did not map the /swagger-ui.html endpoint for me until I added the WebMvcConfig as noted above. One small change though,

.addResourceHandler("/swagger-ui.html")

The code above did not have the /.

Had the same issue, but nothing above helped. Started to wonder if it was a problem with the version 2.7.0. I had also followed this tutorial attempted to adapt it to gradle adding this to build.gradle:

compile group: ‘io.springfox’, name: ‘springfox-swagger-ui’, version: ‘2.7.0’

It seems this was horribly wrong.

While looking at another project of ours I found that we used the following instead for 2.4.0:

compile “io.springfox:springfox-swagger2:${swaggerVersion}” compile “io.springfox:springfox-swagger-ui:${swaggerVersion}” compile “io.springfox:springfox-bean-validators:${swaggerVersion}”

It solved my problem with 2.4.0 so I tried it with 2.7.0 and it also worked. Perhaps, if you are having this problem as a gradle project, this might solve your issue too.

Good luck!

@silentsnooc it is very likely that spring security is interfering with your web jar being visible

@denis111 Any idea what you did?

I’ve been following this tutorial. I added the dependencies

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

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

and created the configuration:

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

from the logs I see that the swagger endpoints are there:

screenshot from 2017-11-05 20-58-45

but like others, I’m getting a 404 when accessing http://localhost:8080/spring-security-rest/api/swagger-ui.html.

@dilipkrish I don’t have to do addResourceHandlers manually, I just wanted swagger-ui to work and I tried this workaround suggested in this topic. Anyway, today I’ve done something(maybe updates something) and now it works without addResourceHandlers. Thanks everybody for support.