Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suddenly Springfox Swagger 3.0 is not working with spring webflux

Tags:

Application was working with Springfox Swagger 3.0 few days back. Suddenly it is stopped working. The Jar file which was created before a week is still working but now when we try to build a new Jar file, which is not working, even without any code/library changes. I have even referred the below URL but still facing issue.

404 error with swagger-ui and spring webflux

Below given my configuration:

POM file:

<properties>     <java.version>1.8</java.version>     <springfox.version>3.0.0-SNAPSHOT</springfox.version>     <spring.version>2.3.1.RELEASE</spring.version> </properties> <repositories>     <repository>         <id>spring-libs-milestone</id>         <name>Spring Milestone Maven Repository</name>         <url>http://oss.jfrog.org/artifactory/oss-snapshot-local/</url>     </repository> </repositories>  <dependencies>     <dependency>         <groupId>org.springframework.boot</groupId>         <artifactId>spring-boot-starter-webflux</artifactId>     </dependency>     <dependency>         <groupId>io.springfox</groupId>         <artifactId>springfox-swagger2</artifactId>         <version>${springfox.version}</version>     </dependency>     <dependency>         <groupId>io.springfox</groupId>         <artifactId>springfox-spring-webflux</artifactId>         <version>${springfox.version}</version>     </dependency>     <dependency>         <groupId>io.springfox</groupId>         <artifactId>springfox-swagger-ui</artifactId>         <version>${springfox.version}</version>     </dependency> </dependencies> 

Config Files:

@Configuration @EnableSwagger2WebFlux public class SwaggerConfiguration implements WebFluxConfigurer {      @Bean     public Docket createRestApi() {         return new Docket(DocumentationType.SWAGGER_2)                 .apiInfo(new ApiInfoBuilder()                         .description("My Reactive API")                         .title("My Domain object API")                         .version("1.0.0")                         .build())                 .enable(true)                 .select()                 .apis(RequestHandlerSelectors.basePackage("com.reactive.controller"))                 .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/");     } } 

I am getting 404 error when I try to open the swagger page.

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

Can someone help me with this. Thanks in advance.

like image 637
Praveen D Avatar asked Jul 07 '20 10:07

Praveen D


People also ask

How do I enable Swagger in spring boot?

To enable the Swagger2 in Spring Boot application, you need to add the following dependencies in our build configurations file. For Gradle users, add the following dependencies in your build. gradle file. Now, add the @EnableSwagger2 annotation in your main Spring Boot application.

Does swagger2 support spring webflux snapshot?

After some research I found that Swagger2 will add support for Spring Webflux in version 3.0.0 But there is already a SNAPSHOT in the wild with support. Add version 3.0.0-SNAPSHOT to your dependencies in your pom.xm

How do I add snapshot to swagger2?

After some research I found that Swagger2 will add support for Spring Webflux in version 3.0.0 But there is already a SNAPSHOT in the wild with support. Add version 3.0.0-SNAPSHOT to your dependencies in your pom.xm Go to or create a SwaggerConfiguration class and add @EnableSwagger2WebFlux to your class

What is the difference between Springfox and springdoc-OpenAPI?

NOTE: The SpringDoc OpenAPI library has more support for Spring reactive API’s built using WebFlux as compared to the SpringFox library. We can also have both SpringFox and Springdoc-openapi enabled as per requirement. 4. Integrating OpenAPI Into the Project 4.1. @OpenAPIDefinition

How to change the default context path of Swagger UI url?

Swagger UI Swagger UI will be exposed at http://localhost:8080/swagger-ui.html#/ by default. NOTE: To change the default context path of swagger UI url, add below property in properties file and provide the required path. 6.1.


Video Answer


2 Answers

The implementation has changed recently (see migrating from earlier snapshots for a brief update on this).

Now the UI is avaiable under /swagger-ui endpoint (not /swagger-ui.html).

You should also drop the @EnableSwagger2WebFlux annotation and addResourceHandlers() method, remove all springfox dependencies and add just one:

<dependency>         <groupId>io.springfox</groupId>         <artifactId>springfox-boot-starter</artifactId>         <version>${springfox.version}</version> </dependency> 
like image 198
jrd Avatar answered Oct 26 '22 04:10

jrd


Getting Started with Swagger-3 in Springboot Rest API

For new projects

For Maven:-

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

For Gradle:-

  implementation "io.springfox:springfox-boot-starter:<version>" 

Now there is no extra configuration to activate swagger on the spring-boot project like the previous. if try to configure with security, there is some configuration. plz refer to this article.

In swagger version 3 remove the @EnableSwagger2 annotation base config also.

And most of the user tries to find HTML swagger document file using {host}/swagger-ui.html or {host}/swagger-ui those are now removed.

use {host}/swagger-ui/ to see the HTML document

This is a sample project link on GitHub Refer to documentation io.springfox

like image 38
LalithK90 Avatar answered Oct 26 '22 03:10

LalithK90