Swagger with Spring Boot 2.0 leads to 404 error page

We Are Going To Discuss About Swagger with Spring Boot 2.0 leads to 404 error page. So lets Start this Java Article.

Swagger with Spring Boot 2.0 leads to 404 error page

  1. Swagger with Spring Boot 2.0 leads to 404 error page

    I was able to make it work with Spring boot version 2.0.4.RELEASE and this blog post:
    I added these dependencies:
    <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency>

  2. Swagger with Spring Boot 2.0 leads to 404 error page

    I was able to make it work with Spring boot version 2.0.4.RELEASE and this blog post:
    I added these dependencies:
    <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency>

Solution 1

I was able to make it work with Spring boot version 2.0.4.RELEASE and this blog post:

I added these dependencies:

<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>

And this configuration file:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

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

And it worked.

The Swagger UI can be reached at /swagger-ui.html#

Original Author riorio Of This Content

Solution 2

Please check the reference: https://springfox.github.io/springfox/docs/current/

“2.1.3. Migrating from existing 2.x version”

You can remove springfox-swagger2 and springfox-swagger-ui from your pom.xml and add springfox-boot-starter instead (for example version 3.0.0). Also you can remove the @EnableSwagger2 annotations

And: “swagger-ui location has moved from http://host/context-path/swagger-ui.html to http://host/context-path/swagger-ui/index.html OR http://host/context-path/swagger-ui/ for short. This makes it work much better with pulling it as a web jar and turning it off using configuration properties if not needed.”

Original Author doct0re Of This Content

Solution 3

First add SwaggerConfig.java file at the same package of your springboot file like the following example.

@Configuration
@EnableSwagger2
@EnableWebMvc
public class SwaggerConfig extends WebMvcConfigurerAdapter {                                    
    @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/");
   }

}

try this
http://localhost:8080/spring-security-rest/api/swagger-ui.html
or
http://localhost:8080/spring-security-rest/swagger-ui.html

If that does not work, try to change the path in application.properties

Add this to application.properties:

server.servlet-path=/loop-service

and try the following urls:

http://localhost:8080/loop-service/swagger-ui.html (UI Docs)

http://localhost:8080/loop-service/v2/api-docs (JSON Docs)

Result :
enter image description here

Original Author Exel Staderlin Of This Content

Solution 4

Just use springdoc-openapi-ui instead.

The dependency:

<dependency>
     <groupId>org.springdoc</groupId>
     <artifactId>springdoc-openapi-ui</artifactId>
     <version>1.5.9</version>
</dependency>

And then for the UI just go to:

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

For the Json go to:

  • http://localhost:8080/v3/api-docs

For yaml:

  • http://localhost:8080/api-docs.yaml

That’s really all there is to it… No annotations / configuration needed. Cheers!

If you are using spring security, make sure you can reach these paths for it to work:

  • /swagger-ui.html
  • /swagger-ui/**
  • /v3/api-docs/**

For more information:
https://www.baeldung.com/spring-rest-openapi-documentation

Original Author H3AR7B3A7 Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment