如何使用springdoc-openapi将Open API 3与Spring项目(而非Spring Boot)集成 [英] How to Integrate Open API 3 with Spring project (not Spring Boot) using springdoc-openapi

查看:112
本文介绍了如何使用springdoc-openapi将Open API 3与Spring项目(而非Spring Boot)集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现有的项目位于Spring Framework而非Spring Boot上.

My Existing Project is on Spring Framework not Spring Boot.

我想将Open API 3与之集成.

I want to integrate Open API 3 with it.

我想使用springdoc-openapi而不使用Jersey进行集成.

I want to integrate using springdoc-openapi not using Jersey.

推荐答案

使用springdoc-openapi-ui 1.5.2的spring-mvc(5.3.1)的ui加载看起来更简单:

The ui load for spring-mvc (5.3.1) using springdoc-openapi-ui 1.5.2 looks more simple:

build.gradle(版本6.5)

build.gradle (gradle version 6.5)

implementation 'org.springdoc:springdoc-openapi-ui:1.5.2'

在依赖项部分中不需要显式地需要spring-boot-autoconfigure和spring-boot,因为org.springdoc:springdoc-openapi-ui:1.5.2已经拥有了两者(版本2.4.0).您会惊讶于最终应用程序中将添加多少以及哪些依赖项.

The spring-boot-autoconfigure and spring-boot are not needed explicitly in dependencies section cause org.springdoc:springdoc-openapi-ui:1.5.2 already has them both (version 2.4.0). You'll be surprised how many and what dependencies will be added to your final application.

OpenApiConfig.java

OpenApiConfig.java

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"org.springdoc"})
@Import({org.springdoc.core.SpringDocConfiguration.class,
         org.springdoc.webmvc.core.SpringDocWebMvcConfiguration.class,
         org.springdoc.webmvc.ui.SwaggerConfig.class,
         org.springdoc.core.SwaggerUiConfigProperties.class,
         org.springdoc.core.SwaggerUiOAuthProperties.class,
         org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration.class})

class OpenApiConfig implements WebMvcConfigurer {
}

组件扫描应覆盖OpenApiConfig软件包.

The OpenApiConfig package should be covered by component scan.

在使用Spring Security的情况下,您可以添加两个URL模式并在代码中定义OpenAPI页面访问的角色.

In case of Spring Security usage, you might add two url patterns and define the role in your code for the OpenAPI pages access.

<security:intercept-url pattern="/swagger*/**" access="ROLE_DEVELOPER"/>
<security:intercept-url pattern="/v3/api-docs" access="ROLE_DEVELOPER"/>

要检查的网址:

http://localhost:8080/your_context_path/swagger-ui.html
http://localhost:8080/your_context_path/v3/api-docs

这篇关于如何使用springdoc-openapi将Open API 3与Spring项目(而非Spring Boot)集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆