OpenAPI3&Spring Boot 自定义配置
作者:互联网
@
目录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,return,String,自定义,Spring,private,docProperties,components,new 来源: https://www.cnblogs.com/yaoguohh/p/16332660.html