|
@ -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; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|