This commit is contained in:
+19
-2
@@ -1,5 +1,7 @@
|
||||
package top.crushtj.framework.common.constant;
|
||||
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
/**
|
||||
* @Title: DateConstants
|
||||
* @Description: 日期常量
|
||||
@@ -8,7 +10,22 @@ package top.crushtj.framework.common.constant;
|
||||
*/
|
||||
public interface DateConstants {
|
||||
/**
|
||||
* 年-月-日 时:分:秒
|
||||
* DateTimeFormatter:年-月-日 时:分:秒
|
||||
*/
|
||||
String Y_M_D_H_M_S_FORMAT = "yyyy-MM-dd HH:mm:ss";
|
||||
DateTimeFormatter DATE_FORMAT_Y_M_D_H_M_S = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||
|
||||
/**
|
||||
* DateTimeFormatter:年-月-日
|
||||
*/
|
||||
DateTimeFormatter DATE_FORMAT_Y_M_D = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
||||
|
||||
/**
|
||||
* DateTimeFormatter:时:分:秒
|
||||
*/
|
||||
DateTimeFormatter DATE_FORMAT_H_M_S = DateTimeFormatter.ofPattern("HH:mm:ss");
|
||||
|
||||
/**
|
||||
* DateTimeFormatter:年-月
|
||||
*/
|
||||
DateTimeFormatter DATE_FORMAT_Y_M = DateTimeFormatter.ofPattern("yyyy-MM");
|
||||
}
|
||||
|
||||
+13
-18
@@ -1,17 +1,10 @@
|
||||
package top.crushtj.framework.common.utils;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
|
||||
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
|
||||
|
||||
import lombok.SneakyThrows;
|
||||
import top.crushtj.framework.common.constant.DateConstants;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -21,24 +14,26 @@ import top.crushtj.framework.common.constant.DateConstants;
|
||||
* @Date: 2025/11/21
|
||||
*/
|
||||
public class JsonUtils {
|
||||
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
||||
private static ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
||||
|
||||
static {
|
||||
OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
OBJECT_MAPPER.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
|
||||
|
||||
// JavaTimeModule 用于指定序列化和反序列化规则
|
||||
JavaTimeModule javaTimeModule = new JavaTimeModule();
|
||||
|
||||
// 支持 LocalDateTime
|
||||
javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DateConstants.Y_M_D_H_M_S_FORMAT)));
|
||||
javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DateConstants.Y_M_D_H_M_S_FORMAT)));
|
||||
|
||||
OBJECT_MAPPER.registerModules(javaTimeModule); // 解决 LocalDateTime 的序列化问题
|
||||
OBJECT_MAPPER.registerModules(new JavaTimeModule());
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化:统一使用 Spring Boot 个性化配置的 ObjectMapper
|
||||
*
|
||||
* @param objectMapper
|
||||
*/
|
||||
public static void init(ObjectMapper objectMapper) {
|
||||
OBJECT_MAPPER = objectMapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将对象转换为 JSON 字符串
|
||||
*
|
||||
*
|
||||
* @param obj 要转换的对象
|
||||
* @return JSON 字符串
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user