generated from mingliqiye/lib-tem
refactor(autoconfigure): 重构自动配置并添加 Jackson 支持- 重新组织 AutoConfiguration 类结构,优化初始化逻辑
All checks were successful
Gitea Actions Build / Build (push) Successful in 1m0s
All checks were successful
Gitea Actions Build / Build (push) Successful in 1m0s
- 新增 JacksonAutoConfiguration 类,专门处理 Jackson 相关配置 - 更新版本号至 3.1.1 - 为 SimpleModule 设置自定义名称 - 移除冗余代码,提高代码可读性和维护性
This commit is contained in:
parent
5bf5cf9c7f
commit
77d60b38fc
@ -22,4 +22,4 @@
|
||||
JDKVERSIONS=1.8
|
||||
GROUPSID=com.mingliqiye.utils
|
||||
ARTIFACTID=mingli-utils
|
||||
VERSIONS=3.1.0
|
||||
VERSIONS=3.1.1
|
||||
|
||||
@ -22,25 +22,18 @@
|
||||
|
||||
package com.mingliqiye.utils.springboot.autoconfigure;
|
||||
|
||||
import com.mingliqiye.utils.bean.springboot.SpringBeanUtil;
|
||||
import com.mingliqiye.utils.collection.ForEach;
|
||||
import com.mingliqiye.utils.jackson.Serializers;
|
||||
import com.mingliqiye.utils.json.JacksonJsonApi;
|
||||
import com.mingliqiye.utils.json.JsonApi;
|
||||
import com.mingliqiye.utils.time.DateTime;
|
||||
import com.mingliqiye.utils.time.Formatter;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
@Configuration
|
||||
@org.springframework.boot.autoconfigure.AutoConfiguration
|
||||
@EnableConfigurationProperties(AutoConfiguration.class)
|
||||
@ComponentScan(
|
||||
{
|
||||
@ -60,11 +53,16 @@ public class AutoConfiguration {
|
||||
"| $$ |\\$ /$$ |$$ | $$ | $$ | $$ | $$\\ $$ | |\n" +
|
||||
"| $$ | \\_/ $$ |$$$$$$$$\\\\$$$$$$ | $$ | \\$$$$$$ | |\n" +
|
||||
"| \\__| \\__|\\________|\\______/ \\__| \\______/ |\n";
|
||||
private static String banner2;
|
||||
private final Logger log = LoggerFactory.getLogger(AutoConfiguration.class);
|
||||
private com.fasterxml.jackson.databind.ObjectMapper objectMapper;
|
||||
|
||||
public AutoConfiguration() {}
|
||||
private static String banner2;
|
||||
private final Logger log = LoggerFactory.getLogger(
|
||||
"MingliUtils-AutoConfiguration"
|
||||
);
|
||||
|
||||
public AutoConfiguration() {
|
||||
printBanner();
|
||||
log.info("MingliUtils AutoConfiguration succeed");
|
||||
}
|
||||
|
||||
public static void printBanner() {
|
||||
StringBuilder bannerBuilder = new StringBuilder(banner);
|
||||
@ -111,37 +109,4 @@ public class AutoConfiguration {
|
||||
"---------------------------------------------------------"
|
||||
);
|
||||
}
|
||||
|
||||
@jakarta.annotation.PostConstruct
|
||||
public void init() {
|
||||
try {
|
||||
Class.forName("com.fasterxml.jackson.databind.ObjectMapper");
|
||||
log.info("init ObjectMapper");
|
||||
objectMapper = SpringBeanUtil.getBean(
|
||||
com.fasterxml.jackson.databind.ObjectMapper.class
|
||||
);
|
||||
Serializers.addSerializers(objectMapper);
|
||||
log.info("add ObjectMapper Serializers OK");
|
||||
} catch (ClassNotFoundException ignored) {
|
||||
log.info(
|
||||
"Jackson ObjectMapper not found in classpath. Jackson serialization features will be disabled."
|
||||
);
|
||||
} catch (Exception e) {
|
||||
log.warn("Failed to initialize ObjectMapper", e);
|
||||
}
|
||||
printBanner();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnClass(name = "com.fasterxml.jackson.databind.ObjectMapper")
|
||||
public JsonApi jsonApi() {
|
||||
if (objectMapper == null) {
|
||||
log.warn(
|
||||
"ObjectMapper is not available, returning null for JsonApi"
|
||||
);
|
||||
return null;
|
||||
}
|
||||
log.info("Creating JacksonJsonApi bean");
|
||||
return new JacksonJsonApi(objectMapper);
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,38 @@
|
||||
package com.mingliqiye.utils.springboot.autoconfigure;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.mingliqiye.utils.jackson.Serializers;
|
||||
import com.mingliqiye.utils.json.JacksonJsonApi;
|
||||
import com.mingliqiye.utils.json.JsonApi;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
@AutoConfiguration
|
||||
@AutoConfigureAfter(
|
||||
name = {
|
||||
"org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration",
|
||||
}
|
||||
)
|
||||
@ConditionalOnClass(ObjectMapper.class)
|
||||
public class JacksonAutoConfiguration {
|
||||
|
||||
private final Logger log = LoggerFactory.getLogger(
|
||||
"MingliUtils-JacksonAutoConfiguration"
|
||||
);
|
||||
|
||||
public JacksonAutoConfiguration() {
|
||||
log.info("MingliUtils JacksonAutoConfiguration succeed");
|
||||
}
|
||||
|
||||
@Bean
|
||||
public JsonApi jsonApi(ObjectMapper objectMapper) {
|
||||
log.info("Creating JacksonJsonApi bean");
|
||||
Serializers.addSerializers(objectMapper);
|
||||
log.info("MingliUtils Serializers created");
|
||||
return new JacksonJsonApi(objectMapper);
|
||||
}
|
||||
}
|
||||
@ -31,6 +31,7 @@ import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
|
||||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||
import com.mingliqiye.utils.time.DateTime;
|
||||
import com.mingliqiye.utils.time.Formatter;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
@ -47,7 +48,7 @@ public class Jackson {
|
||||
*/
|
||||
public static void addSerializers(ObjectMapper objectMapper) {
|
||||
// 创建SimpleModule并添加DateTime类型的序列化器和反序列化器
|
||||
SimpleModule module = new SimpleModule()
|
||||
SimpleModule module = new SimpleModule("MingliqiyeDateTimeModule")
|
||||
.addSerializer(DateTime.class, new DateTimeJsonSerializer())
|
||||
.addDeserializer(DateTime.class, new DateTimeJsonDeserializerM7());
|
||||
objectMapper.registerModule(module);
|
||||
|
||||
@ -47,7 +47,7 @@ public class Jackson {
|
||||
*/
|
||||
public static void addSerializers(ObjectMapper objectMapper) {
|
||||
// 创建SimpleModule并添加UUID的序列化器和反序列化器
|
||||
SimpleModule module = new SimpleModule()
|
||||
SimpleModule module = new SimpleModule("MingliqiyeUUIDModule")
|
||||
.addSerializer(UUID.class, new UUIDJsonSerializer())
|
||||
.addDeserializer(UUID.class, new UUIDJsonDeserializer());
|
||||
objectMapper.registerModule(module);
|
||||
|
||||
@ -21,3 +21,4 @@
|
||||
#
|
||||
|
||||
com.mingliqiye.utils.springboot.autoconfigure.AutoConfiguration
|
||||
com.mingliqiye.utils.springboot.autoconfigure.JacksonAutoConfiguration
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user