refactor: 适配 Jackson、API 文档(Knife4j:Spring Doc)自动配置

This commit is contained in:
Charles7c 2023-11-21 22:07:53 +08:00
parent ec1daaf045
commit a86f3a5047
7 changed files with 10 additions and 168 deletions

View File

@ -17,10 +17,16 @@
<description>公共模块(存放公共工具类,公共配置等)</description>
<dependencies>
<!-- ContiNew Starter 核心依赖 -->
<!-- ContiNew Starter API 文档依赖 -->
<dependency>
<groupId>top.charles7c.continew</groupId>
<artifactId>continew-starter-core</artifactId>
<artifactId>continew-starter-api-doc</artifactId>
</dependency>
<!-- ContiNew Starter Jackson 依赖 -->
<dependency>
<groupId>top.charles7c.continew</groupId>
<artifactId>continew-starter-json-jackson</artifactId>
</dependency>
<!-- ################ Spring Boot 相关 ################ -->
@ -108,12 +114,6 @@
<artifactId>mica-ip2region</artifactId>
</dependency>
<!-- Knife4j前身是 swagger-bootstrap-ui集 Swagger2 和 OpenAPI3 为一体的增强解决方案) -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId>
</dependency>
<!-- Redisson不仅仅是一个 Redis Java 客户端) -->
<dependency>
<groupId>org.redisson</groupId>

View File

@ -1,72 +0,0 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.cnadmin.common.config;
import lombok.RequiredArgsConstructor;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import org.springdoc.core.customizers.GlobalOpenApiCustomizer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.RandomUtil;
import top.charles7c.cnadmin.common.config.properties.ProjectProperties;
/**
* 接口文档配置
*
* @author Charles7c
* @since 2022/12/11 19:14
*/
@Configuration
@RequiredArgsConstructor
@ConditionalOnProperty(name = "springdoc.swagger-ui.enabled", havingValue = "true", matchIfMissing = true)
public class SwaggerConfiguration {
private final ProjectProperties projectProperties;
/**
* 接口文档配置
*/
@Bean
public OpenAPI openApi() {
return new OpenAPI().info(new Info().title(projectProperties.getName() + " 接口文档")
.version(String.format("v%s", projectProperties.getVersion()))
.description(projectProperties.getDescription()).termsOfService(projectProperties.getUrl())
.contact(projectProperties.getAuthor()).license(projectProperties.getLicense()));
}
/**
* 根据 @Tag 上的排序写入 x-order
*
* @return the global open api customizer
*/
@Bean
public GlobalOpenApiCustomizer orderGlobalOpenApiCustomizer() {
return openApi -> {
if (null != openApi.getTags()) {
openApi.getTags()
.forEach(tag -> tag.setExtensions(MapUtil.of("x-order", RandomUtil.randomInt(0, 100))));
}
};
}
}

View File

@ -60,10 +60,6 @@ public class WebMvcConfiguration implements WebMvcConfigurer {
.setCachePeriod(0);
registry.addResourceHandler(localStorageProperties.getAvatarPattern()).addResourceLocations(avatarUtl)
.setCachePeriod(0);
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());
}
/**

View File

@ -53,30 +53,6 @@ import top.charles7c.cnadmin.common.base.IBaseEnum;
@Configuration
public class JacksonConfiguration {
/**
* 针对时间类型LocalDateTimeLocalDateLocalTime 的序列化和反序列化
*/
@Bean
public Jackson2ObjectMapperBuilderCustomizer customizer() {
return builder -> {
JavaTimeModule javaTimeModule = new JavaTimeModule();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN);
javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(dateTimeFormatter));
javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(dateTimeFormatter));
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN);
javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(dateFormatter));
javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(dateFormatter));
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern(DatePattern.NORM_TIME_PATTERN);
javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(timeFormatter));
javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(timeFormatter));
builder.timeZone(TimeZone.getDefault());
builder.modules(javaTimeModule);
log.info(">>>初始化 Jackson 序列化配置<<<");
};
}
/**
* 针对枚举基类 BaseEnum 的序列化和反序列化
*/

View File

@ -36,55 +36,7 @@ import cn.hutool.extra.spring.SpringUtil;
*/
@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 author;
/**
* 许可协议信息
*/
@NestedConfigurationProperty
private License license;
/**
* 是否为生产环境
*/
private boolean production = false;
public class ProjectProperties extends top.charles7c.continew.starter.core.autoconfigure.ProjectProperties {
/**
* 是否本地解析 IP 归属地

View File

@ -11,7 +11,7 @@ project:
# 基本包
basePackage: top.charles7c.cnadmin
## 作者信息配置
author:
contact:
name: Charles7c
email: charles7c@126.com
url: https://blog.charles7c.top/about/me

10
pom.xml
View File

@ -40,7 +40,6 @@
<justauth.version>1.16.5</justauth.version>
<easyexcel.version>3.3.2</easyexcel.version>
<ip2region.version>3.1.5.1</ip2region.version>
<knife4j.version>4.3.0</knife4j.version>
<redisson.version>3.24.3</redisson.version>
<easy-captcha.version>1.6.2</easy-captcha.version>
<hutool.version>5.8.22</hutool.version>
@ -139,15 +138,6 @@
<version>${ip2region.version}</version>
</dependency>
<!-- Knife4j前身是 swagger-bootstrap-ui集 Swagger2 和 OpenAPI3 为一体的增强解决方案) -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-dependencies</artifactId>
<version>${knife4j.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Redisson不仅仅是一个 Redis Java 客户端) -->
<dependency>
<groupId>org.redisson</groupId>