PaperBasicInfo
parent
32ab24c8a0
commit
e17af6a4a7
|
|
@ -0,0 +1,47 @@
|
|||
package org.qinan.cedu.config;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
/**
|
||||
* 兼容多种格式的 LocalDateTime 反序列化器:
|
||||
* 解决 @JsonFormat(pattern = "yyyy-MM-dd") 配在 LocalDateTime 上时,
|
||||
* 纯日期字符串无法构造 LocalDateTime 导致的反序列化失败问题。
|
||||
*/
|
||||
public class FlexibleLocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {
|
||||
|
||||
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||
|
||||
/**
|
||||
* 将 JSON 字符串反序列化为 LocalDateTime
|
||||
* 支持格式:yyyy-MM-dd(补 00:00:00)、yyyy-MM-dd HH:mm:ss、ISO 格式(yyyy-MM-ddTHH:mm:ss)
|
||||
*
|
||||
* @param p JSON 解析器
|
||||
* @param ctxt 反序列化上下文
|
||||
* @return 解析后的 LocalDateTime,空字符串返回 null
|
||||
* @throws IOException 读取失败或格式非法时抛出
|
||||
*/
|
||||
@Override
|
||||
public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
|
||||
String text = p.getText();
|
||||
if (text == null || text.trim().isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
text = text.trim();
|
||||
if (text.length() == 10) {
|
||||
// 纯日期字符串,时间部分补 00:00:00
|
||||
return LocalDate.parse(text).atStartOfDay();
|
||||
}
|
||||
if (text.contains("T")) {
|
||||
// ISO 格式
|
||||
return LocalDateTime.parse(text);
|
||||
}
|
||||
return LocalDateTime.parse(text, DATE_TIME_FORMATTER);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,8 +1,11 @@
|
|||
package org.qinan.cedu.model.dto;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import org.qinan.cedu.config.FlexibleLocalDateTimeDeserializer;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
|
@ -42,13 +45,11 @@ public class ClassManagementDTO {
|
|||
private String safetyManager;
|
||||
|
||||
@ApiModelProperty("培训开始日期")
|
||||
@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
|
||||
@DateTimeFormat(pattern = "yyyy-MM-dd")
|
||||
@JsonDeserialize(using = FlexibleLocalDateTimeDeserializer.class)
|
||||
private LocalDateTime startDate;
|
||||
|
||||
@ApiModelProperty("培训结束日期")
|
||||
@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
|
||||
@DateTimeFormat(pattern = "yyyy-MM-dd")
|
||||
@JsonDeserialize(using = FlexibleLocalDateTimeDeserializer.class)
|
||||
private LocalDateTime endDate;
|
||||
|
||||
@ApiModelProperty("是否开启考试(0-否,1-是)")
|
||||
|
|
|
|||
Loading…
Reference in New Issue