How to use OpenApi annotations in spring-webflux RouterFunction endpoints?

Viewed 6078

I am currently working on a project where I use spring functional web programming. I usually use annotations of swagger 2 in restController but with functional web programming I can not find where ! The place to tell the app to do a search for endpoints (like basepackage in Docket) and load swagger in an html page. Here is my code:

@Configuration
public class RouterClient{

@Bean
public RouterFunction<ServerResponse> routes(ClientHandler client){
  return route(GET("/api/client"), client::findAll)
      .andRoute(POST("/api/client"),client::add);
  }
}

Config Class:

@Configuration
public class OpenApiConfiguration{

  @Bean
    public GroupedOpenApi groupOpenApi() {
        String paths[] = {"/api/**"};
        String packagesToscan[] = {"com.demo.client"};
        return GroupedOpenApi.builder().setGroup("groups").pathsToMatch(paths).packagesToScan(packagesToscan)
                .build();
    }

}

The dependencies:

    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-webflux-core</artifactId>
        <version>1.2.32</version>
    </dependency>
    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-webflux-ui</artifactId>
        <version>1.2.32</version>
    </dependency>

The result :

enter image description here

4 Answers

Functional endpoints are supported since 1.3.8 (early May). See releases on GitHub.

Have a look at this: https://springdoc.org/#spring-webfluxwebmvc-fn-with-functional-endpoints

The easiest way to see your endpoints on the Swagger UI is to add the @RouterOperation annotation to your RouterFunction methods (containing a single route), and specify the beanClass and beanMethod used in it. However, in your case, there are multiple routes on a single method, so you must also use the @RouterOperations annotation. These cases are well documented in the link above.

It seems like the current implementation of springdoc-openapi only allows to manually add the documentation.

set

springdoc.api-docs.enabled=false

This will skip classpath scanning (springfox) for the API annotations. (OAS3 replaced these in v3) and replace them with reading in the spec file (json/yaml).

Put the documentation in the Spec files, as one can generate any number of clients from these. Easiest way to start with legacy code is to copy the /api-docs/ files generated by springfox.

You can go to editor.swagger.io, load in the version 2 yaml and convert it to version 3 if springfox still doesn't do that. Then work with yaml files. (it's a contract UP-Front specification for a reason)

spring-webflux with Functional Endpoints, will be available in the future release

Related