Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate webservice description with Swagger in Spring Webflux

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.

like image 606
Jimmy Pannier Avatar asked Jan 18 '18 09:01

Jimmy Pannier


1 Answers

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>  
like image 93
Niraj Sonawane Avatar answered Oct 21 '22 07:10

Niraj Sonawane