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'

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐