From 7973f6c2dc7dc5defdec16bd3eb7885ecb593554 Mon Sep 17 00:00:00 2001 From: Charles7c Date: Tue, 21 Nov 2023 22:05:43 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=20API=20=E6=96=87?= =?UTF-8?q?=E6=A1=A3=EF=BC=88Knife4j=EF=BC=9ASpring=20Doc=EF=BC=89?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- continew-starter-api-doc/pom.xml | 31 +++++ .../SpringDocAutoConfiguration.java | 72 +++++++++++ .../StaticResourceAutoConfiguration.java | 46 +++++++ ...ot.autoconfigure.AutoConfiguration.imports | 2 + .../core/autoconfigure/ProjectProperties.java | 118 ++++++++++++++++++ continew-starter-dependencies/pom.xml | 17 +++ pom.xml | 1 + 7 files changed, 287 insertions(+) create mode 100644 continew-starter-api-doc/pom.xml create mode 100644 continew-starter-api-doc/src/main/java/top/charles7c/continew/starter/apidoc/autoconfigure/SpringDocAutoConfiguration.java create mode 100644 continew-starter-api-doc/src/main/java/top/charles7c/continew/starter/apidoc/autoconfigure/StaticResourceAutoConfiguration.java create mode 100644 continew-starter-api-doc/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports create mode 100644 continew-starter-core/src/main/java/top/charles7c/continew/starter/core/autoconfigure/ProjectProperties.java diff --git a/continew-starter-api-doc/pom.xml b/continew-starter-api-doc/pom.xml new file mode 100644 index 00000000..1467ee65 --- /dev/null +++ b/continew-starter-api-doc/pom.xml @@ -0,0 +1,31 @@ + + + 4.0.0 + + top.charles7c.continew + continew-starter + ${revision} + + + continew-starter-api-doc + jar + + ${project.artifactId} + ContiNew Starter API 文档模块 + + + + + com.github.xiaoymin + knife4j-openapi3-jakarta-spring-boot-starter + + + + + top.charles7c.continew + continew-starter-core + + + \ No newline at end of file diff --git a/continew-starter-api-doc/src/main/java/top/charles7c/continew/starter/apidoc/autoconfigure/SpringDocAutoConfiguration.java b/continew-starter-api-doc/src/main/java/top/charles7c/continew/starter/apidoc/autoconfigure/SpringDocAutoConfiguration.java new file mode 100644 index 00000000..30cf6cf7 --- /dev/null +++ b/continew-starter-api-doc/src/main/java/top/charles7c/continew/starter/apidoc/autoconfigure/SpringDocAutoConfiguration.java @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package top.charles7c.continew.starter.apidoc.autoconfigure; + +import io.swagger.v3.oas.models.OpenAPI; +import io.swagger.v3.oas.models.info.Contact; +import io.swagger.v3.oas.models.info.Info; +import io.swagger.v3.oas.models.info.License; +import jakarta.annotation.PostConstruct; +import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Lazy; +import top.charles7c.continew.starter.core.autoconfigure.ProjectProperties; + +/** + * API 文档自动配置 + * + * @author Charles7c + * @since 1.0.0 + */ +@Slf4j +@Lazy +@Configuration(proxyBeanMethods = false) +@ConditionalOnProperty(name = "springdoc.swagger-ui.enabled", havingValue = "true") +public class SpringDocAutoConfiguration { + + /** + * Open API 配置 + */ + @Bean + @ConditionalOnMissingBean + public OpenAPI openApi(ProjectProperties properties) { + Info info = new Info() + .title(String.format("%s %s", properties.getName(), "API 文档")) + .version(properties.getVersion()) + .description(properties.getDescription()); + ProjectProperties.Contact contact = properties.getContact(); + if (null != contact) { + info.contact(new Contact().name(contact.getName()) + .email(contact.getEmail()) + .url(contact.getUrl())); + } + ProjectProperties.License license = properties.getLicense(); + if (null != license) { + info.license(new License().name(license.getName()) + .url(license.getUrl())); + } + return new OpenAPI().info(info); + } + + @PostConstruct + public void postConstruct() { + log.info("[ContiNew Starter] - Auto Configuration 'ApiDoc' completed initialization."); + } +} diff --git a/continew-starter-api-doc/src/main/java/top/charles7c/continew/starter/apidoc/autoconfigure/StaticResourceAutoConfiguration.java b/continew-starter-api-doc/src/main/java/top/charles7c/continew/starter/apidoc/autoconfigure/StaticResourceAutoConfiguration.java new file mode 100644 index 00000000..7b1d020c --- /dev/null +++ b/continew-starter-api-doc/src/main/java/top/charles7c/continew/starter/apidoc/autoconfigure/StaticResourceAutoConfiguration.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package top.charles7c.continew.starter.apidoc.autoconfigure; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.CacheControl; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +import java.util.concurrent.TimeUnit; + +/** + * 静态资源处理器自动配置 + * + * @author Charles7c + * @since 1.0.0 + */ +@EnableWebMvc +@Configuration(proxyBeanMethods = false) +@ConditionalOnProperty(name = "springdoc.swagger-ui.enabled", havingValue = "true") +public class StaticResourceAutoConfiguration implements WebMvcConfigurer { + + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.addResourceHandler("/favicon.ico").addResourceLocations("classpath:/"); + registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/"); + registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/") + .setCacheControl(CacheControl.maxAge(5, TimeUnit.HOURS).cachePublic()); + } +} diff --git a/continew-starter-api-doc/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/continew-starter-api-doc/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 00000000..e48225e8 --- /dev/null +++ b/continew-starter-api-doc/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1,2 @@ +top.charles7c.continew.starter.apidoc.autoconfigure.SpringDocAutoConfiguration +top.charles7c.continew.starter.apidoc.autoconfigure.StaticResourceAutoConfiguration \ No newline at end of file diff --git a/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/autoconfigure/ProjectProperties.java b/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/autoconfigure/ProjectProperties.java new file mode 100644 index 00000000..c2b52390 --- /dev/null +++ b/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/autoconfigure/ProjectProperties.java @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package top.charles7c.continew.starter.core.autoconfigure; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.NestedConfigurationProperty; +import org.springframework.stereotype.Component; + +/** + * 项目配置属性 + * + * @author Charles7c + * @since 1.0.0 + */ +@Data +@Component +@ConfigurationProperties(prefix = "project") +public class ProjectProperties { + + /** + * 名称 + */ + private String name; + + /** + * 应用名称 + */ + private String appName; + + /** + * 版本 + */ + private String version; + + /** + * 描述 + */ + private String description; + + /** + * URL + */ + private String url; + + /** + * 基本包 + */ + private String basePackage; + + /** + * 联系人 + */ + @NestedConfigurationProperty + private Contact contact; + + /** + * 许可协议 + */ + @NestedConfigurationProperty + private License license; + + /** + * 是否为生产环境 + */ + private boolean production = false; + + /** + * 联系人配置属性 + */ + @Data + public static class Contact { + /** + * 名称 + */ + private String name; + + /** + * 邮箱 + */ + private String email; + + /** + * URL + */ + private String url; + } + + /** + * 许可协议配置属性 + */ + @Data + public static class License { + /** + * 名称 + */ + private String name; + + /** + * URL + */ + private String url; + } +} diff --git a/continew-starter-dependencies/pom.xml b/continew-starter-dependencies/pom.xml index f5f5b0c1..0953eb3b 100644 --- a/continew-starter-dependencies/pom.xml +++ b/continew-starter-dependencies/pom.xml @@ -55,11 +55,21 @@ 1.0.0-SNAPSHOT + 4.3.0 5.8.23 + + + com.github.xiaoymin + knife4j-dependencies + ${knife4j.version} + pom + import + + cn.hutool @@ -67,6 +77,13 @@ ${hutool.version} + + + top.charles7c.continew + continew-starter-api-doc + ${revision} + + top.charles7c.continew diff --git a/pom.xml b/pom.xml index 31aa0645..7476ca9b 100644 --- a/pom.xml +++ b/pom.xml @@ -70,6 +70,7 @@ continew-starter-dependencies continew-starter-core continew-starter-json + continew-starter-api-doc