8

Has someone a solution to describe webservice using Swagger library in a Spring webflux environment?

The goal is to use Swagger to generate the ws client stubs automatically.

moffeltje
  • 4,521
  • 4
  • 33
  • 57
Jimmy Pannier
  • 261
  • 1
  • 2
  • 16

6 Answers6

5

Work around until Springfox 3.0.0 in not available

Pom File

       <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>3.0.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-spring-webflux</artifactId>
            <version>3.0.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>3.0.0-SNAPSHOT</version>
        </dependency>

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

Config

@Configuration
@EnableSwagger2WebFlux
public class SwaggerConfig  implements WebFluxConfigurer {
    @Bean
    public Docket api() {
       
        return new Docket(DocumentationType.SWAGGER_2)
                .genericModelSubstitutes(Mono.class, Flux.class, Publisher.class)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler("/swagger**")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}

Update

springfox-boot-starter is available now and it's work with webflux. We just need to add below starter project in pom file. Note: With springfox-boot-starter in class path we do not need @EnableSwagger2WebFlux.

Remove explicit dependencies on springfox-swagger2

<dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>${io.springfox}</version>
        </dependency>  
Niraj Sonawane
  • 10,225
  • 10
  • 75
  • 104
3

Spring boot 2.x and spring 5 features aren’t supported yet by springfox as of 2.8.0.

You might want to subscribe to the following Springfox issue: https://github.com/springfox/springfox/issues/1773

jivimberg
  • 840
  • 1
  • 10
  • 21
napulitano
  • 61
  • 7
2

While waiting for the official webflux support from the springfox project, there is a workaround done by deblockt on GitHub: https://github.com/deblockt/springfox/tree/feature/webflux.

Just checkout the sourecode and build your own jars, include them into your project.

dhqvinh
  • 337
  • 4
  • 9
1

As of release 2.9.2 you can use swagger with webflux.

https://github.com/deblockt/springfox/tree/feature/webflux

Deepak Agrawal
  • 1,301
  • 5
  • 19
  • 43
0

I configured Spring boot 2 Servlet stack with swagger , project in github. Better than nothing. :) https://github.com/armdev/springboot2-swagger

Armen Arzumanyan
  • 1,939
  • 3
  • 30
  • 56
-1

You can use spring-boot-starter-web along with spring-boot-starter-webflux. So if you really want to use swagger with spring webflux, then you must have to add spring-boot-starter-web depdendency into project. You can use swagger now exactly you have used with spring web. There won't be any difference.

Shashank K
  • 210
  • 3
  • 10
  • 2
    If you include webflux and web starters, Spring will by default take in web and use the web mvc variant. This is not a good recommendation. – Bocky Nov 07 '18 at 03:15
  • if you combine both spring starter web and webflux, you will be in danger. – uday214125 Aug 03 '19 at 16:53