spring webflux 中使用 openapi 自动发布apidoc

java 8个月前 will
1.1K 0

spring webflux 中使用 openapi 自动发布apidoc

        <dependency>
            <groupId>io.swagger.core.v3</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>2.1.1</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-webmvc</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

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

如需自定义路径, 可以添加如下配置项:

springdoc:
  api-docs:
    path: /api-docs
    enabled: true
    groups:
      enabled: true
  swagger-ui:
    path: /swagger-ui.html

添加springdoc配置项:


import io.swagger.v3.oas.models.Components; import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.media.Schema; import io.swagger.v3.oas.models.media.StringSchema; import io.swagger.v3.oas.models.parameters.HeaderParameter; import org.springdoc.core.customizers.OpenApiCustomiser; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; import org.springframework.http.HttpStatus; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.web.reactive.function.server.RouterFunction; import org.springframework.web.reactive.function.server.ServerResponse; import org.springframework.web.server.ResponseStatusException; import org.springframework.web.server.WebFilter; import java.net.URI; import static org.springdoc.core.Constants.*; import static org.springdoc.core.Constants.SWAGGER_UI_URL; import static org.springframework.web.reactive.function.server.RequestPredicates.GET; import static org.springframework.web.reactive.function.server.RouterFunctions.route; @Configuration public class DocConfig { @Bean public OpenAPI customOpenAPI() { Schema<String> schema = new Schema<>(); schema.addEnumItemObject("consumer1"); schema.addEnumItemObject("swagger"); schema.addEnumItemObject("consumer3"); schema.setDefault("swagger"); return new OpenAPI() .components(new Components().addParameters("myConsumerTypeHeader", new HeaderParameter().required(true).name("My-Consumer-Type").description("The II Consumer Type").schema(schema))); } @Bean public OpenApiCustomiser consumerTypeHeaderOpenAPICustomiser() { return openApi -> openApi.getPaths().values().stream().flatMap(pathItem -> pathItem.readOperations().stream()) .forEach(operation -> operation.addParametersItem(new HeaderParameter().$ref("#/components/parameters/myConsumerTypeHeader"))); } @Bean public OpenApiCustomiser microtypeOpenAPICustomiser() { return openApi -> openApi.getComponents().addSchemas("TweetId", new StringSchema()); } @Value(API_DOCS_URL) private String apiDocsUrl; @Value(SWAGGER_UI_PATH) private String uiPath; @Value("${server.servlet.context-path}") private String contextPath; @Bean RouterFunction<ServerResponse> routerFunction2() { StringBuilder sbUrl = new StringBuilder(); sbUrl.append(contextPath); sbUrl.append(DEFAULT_WEB_JARS_PREFIX_URL); sbUrl.append(SWAGGER_UI_URL); sbUrl.append(contextPath + apiDocsUrl); // sbUrl.append(DEFAULT_VALIDATOR_URL); return route(GET(contextPath + uiPath), req -> ServerResponse.temporaryRedirect(URI.create(sbUrl.toString())).build()); } @Bean @ConditionalOnProperty("server.servlet.context-path") @Order(Ordered.HIGHEST_PRECEDENCE) public WebFilter contextPathWebFilter(ServerProperties serverProperties) { String contextPath = serverProperties.getServlet().getContextPath(); return (exchange, chain) -> { ServerHttpRequest request = exchange.getRequest(); String requestPath = request.getURI().getPath(); if (requestPath.startsWith(contextPath + "/") || requestPath.equals(contextPath)) { return chain.filter(exchange.mutate().request(request.mutate().contextPath(contextPath).build()).build()); } else { throw new ResponseStatusException(HttpStatus.NOT_FOUND); } }; } }
版权声明:will 发表于 2020-03-23 9:41:27。
转载请注明:spring webflux 中使用 openapi 自动发布apidoc | 敏利雅

暂无评论

您必须登录才能参与评论!
立即登录
暂无评论...