Browse Source

refactor(time): 优化时间格式化和反序列化

- 修改 CmsWebsiteServiceImplHelper 中 createTime 的处理方式,使用 formatter 进行格式化
- 在 JacksonConfig 中配置自定义的 ObjectMapper,确保 LocalDateTime 使用指定格式(yyyy-MM-dd HH:mm:ss)
- 添加自定义的 JavaTimeModule,配置 LocalDateTime 的序列化和反序列化格式
- 禁用将日期写为时间戳,忽略未知属性,并允许空字符串作为 null 对象
pan
科技小王子 3 weeks ago
parent
commit
b43816b875
  1. 4
      src/main/java/com/gxwebsoft/cms/service/impl/CmsWebsiteServiceImplHelper.java
  2. 54
      src/main/java/com/gxwebsoft/common/core/config/JacksonConfig.java

4
src/main/java/com/gxwebsoft/cms/service/impl/CmsWebsiteServiceImplHelper.java

@ -62,7 +62,9 @@ public class CmsWebsiteServiceImplHelper {
vo.setDomain(website.getDomain()); vo.setDomain(website.getDomain());
vo.setRunning(website.getRunning()); vo.setRunning(website.getRunning());
vo.setVersion(website.getVersion()); vo.setVersion(website.getVersion());
vo.setCreateTime(String.valueOf(website.getCreateTime()));
if (website.getCreateTime() != null) {
vo.setCreateTime(website.getCreateTime().format(formatter));
}
// 时间字段 - 格式化为字符串 // 时间字段 - 格式化为字符串
if (website.getExpirationTime() != null) { if (website.getExpirationTime() != null) {

54
src/main/java/com/gxwebsoft/common/core/config/JacksonConfig.java

@ -1,13 +1,22 @@
package com.gxwebsoft.common.core.config; package com.gxwebsoft.common.core.config;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/** /**
* Jackson配置类 * Jackson配置类
* 确保JSR310模块被正确注册
* 确保JSR310模块被正确注册到ObjectMapper中并配置自定义时间格式
* *
* @author WebSoft * @author WebSoft
* @since 2025-01-12 * @since 2025-01-12
@ -15,12 +24,51 @@ import org.springframework.context.annotation.Configuration;
@Configuration @Configuration
public class JacksonConfig { public class JacksonConfig {
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
/**
* 配置ObjectMapper确保JSR310模块被正确注册并设置自定义时间格式
*/
@Bean
@Primary
@ConditionalOnMissingBean
public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
// 创建自定义的JavaTimeModule
JavaTimeModule javaTimeModule = new JavaTimeModule();
// 配置LocalDateTime的序列化和反序列化格式
javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DATE_TIME_FORMATTER));
javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DATE_TIME_FORMATTER));
// 注册自定义的JSR310模块
mapper.registerModule(javaTimeModule);
// 禁用将日期写为时间戳
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
// 忽略未知属性
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// 允许空字符串作为null对象
mapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
return mapper;
}
/** /**
* 确保JavaTimeModule被注册
* 确保JavaTimeModule被注册保留原有Bean
*/ */
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public JavaTimeModule javaTimeModule() { public JavaTimeModule javaTimeModule() {
return new JavaTimeModule();
JavaTimeModule module = new JavaTimeModule();
// 配置LocalDateTime的序列化和反序列化格式
module.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DATE_TIME_FORMATTER));
module.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DATE_TIME_FORMATTER));
return module;
} }
} }

Loading…
Cancel
Save