OpenAPI3&Spring Boot 自定义配置
2022/5/31 23:19:37
本文主要是介绍OpenAPI3&Spring Boot 自定义配置,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
@
目录- DocProperties
- DocConfiguration
- pom.xml
- application.yaml
DocProperties
@Data @ConfigurationProperties("doc.info") public class DocProperties { /** * 分组名称 */ private String group = "default"; /** * 标题 */ private String title = "API"; /** * 描述 */ private String description = "RESTFUL API"; /** * 版 */ private String version = "v2.0.0"; /** * 接口调用地址 */ private String serverUrl; /** * 执照 */ private String license = "Apache 2.0"; /** * 执照地址 */ private String licenseUrl = "https://www.apache.org/licenses/LICENSE-2.0.html"; /** * 全局变量 */ private List<Parameter> globalParameter; }
DocConfiguration
- Doc 基础信息配置
- 认证配置
- 全局变量配置
@Slf4j @AllArgsConstructor @EnableConfigurationProperties(DocProperties.class) public class DocConfiguration { private final DocProperties docProperties; /** * Api docket. * * @return the docket */ @Bean public GroupedOpenApi api() { return GroupedOpenApi.builder() .group(docProperties.getGroup()) .pathsToMatch("/**") .build(); } /** * Open api open api. * * @return the open api */ @Bean public OpenAPI openApi() { return new OpenAPI() .info(new Info() .title(docProperties.getTitle()) .description(docProperties.getDescription()) .version(docProperties.getVersion()) .license(new License().name(docProperties.getLicense()).url(docProperties.getLicenseUrl()))) // 配置接口访问地址 .servers(Collections.singletonList(new Server().url(docProperties.getServerUrl()))) // 配置认证 .security(Collections.singletonList(new SecurityRequirement().addList("Bearer Authorization"))) .components(this.components()); } private Components components() { Components components = new Components() .addSecuritySchemes("Bearer Authorization", new SecurityScheme().type(SecurityScheme.Type.HTTP).scheme("bearer").bearerFormat("JWT")) .addSecuritySchemes("Basic Authorization", new SecurityScheme().type(SecurityScheme.Type.HTTP).scheme("basic")); docProperties.getGlobalParameter() .forEach(parameter -> components.addParameters(parameter.getName(), parameter)); return components; } }
pom.xml
<properties> <springdoc.version>1.6.8</springdoc.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> </dependency> <!-- SpringDoc --> <dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-ui</artifactId> <version>${springdoc.version}</version> </dependency> </dependencies>
application.yaml
doc: info: title: '系统管理服务' description: '系统管理服务 RESTFUL API' server-url: ${DOC_SERVER_URL:http://127.0.0.1:${server.port}} global-parameter: - name: 'realm' in: 'header' schema: type: 'string'
这篇关于OpenAPI3&Spring Boot 自定义配置的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23Springboot应用的多环境打包入门
- 2024-11-23Springboot应用的生产发布入门教程
- 2024-11-23Python编程入门指南
- 2024-11-23Java创业入门:从零开始的编程之旅
- 2024-11-23Java创业入门:新手必读的Java编程与创业指南
- 2024-11-23Java对接阿里云智能语音服务入门详解
- 2024-11-23Java对接阿里云智能语音服务入门教程
- 2024-11-23JAVA对接阿里云智能语音服务入门教程
- 2024-11-23Java副业入门:初学者的简单教程
- 2024-11-23JAVA副业入门:初学者的实战指南