Unable to integrate swagger in my project, getting fallback error

61 views Asked by At

I was trying to integerate swagger in my code by going through a guide on medium. But I am getting the whitelabel error page. Error page

My code snippets below. Application.properties:

spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
springfox.documentation.swagger-ui.enabled=true
server.servlet.contextPath=/swagger-u

Swaggerconfig.Java:

@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api(){
return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.basePackage("com.ecommerce.app")).paths(PathSelectors.ant("/*")).build();
}
}

Webconfig.java:

@Configuration
public class WebConfig {
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("*").allowedMethods("GET", "PUT", "POST", "PATCH", "DELETE");
}
};
}
}

Pom.xml:

<properties>
<java.version>17</java.version>
<org.mapstruct.version>1.5.5.Final</org.mapstruct.version>
</properties>
<dependencies>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.12</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.5.5.Final</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source> <!-- depending on your project -->
<target>1.8</target> <!-- depending on your project -->
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
<!-- other annotation processors -->
</annotationProcessorPaths>
</configuration>

I have tried using localhost:8080/swagger-ui.html and swagger-ui/index.html, but haven't been able to get anything fruitful. Any suggestions will be appreciated.

1

There are 1 answers

0
Ch Vamsi On BEST ANSWER

if you are trying to integrate swagger-ui to your service use OpenApi. Add this dependency in pom.xml :

<dependency>
   <groupId>org.springdoc</groupId>
   <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
   <version>2.1.0</version>
</dependency>

No need to add any configuration in application.yml / application.properties. if you want to add custom description content , you can implement SwaggerConfig, it's optional. Code for your reference:

@OpenAPIDefinition
@Configuration
public class SwaggerConfig {
    @Bean
    public OpenAPI baseOpenAPI(){

        return new OpenAPI()
                .info(new Info()
                        .title("My-MicroService OpenAPI Docs")
                        .version("1.0.0").description("Available API's in My Microservice"));
    }

}