Commit a531d308 authored by 王蔡翾's avatar 王蔡翾
Browse files

bug修复

parent b5ce9f24
package com.platform.business.model.dto;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.format.ResolverStyle;
import java.util.Date;
/**
* 请假表单专用时间解析器,兼容页面分钟精度和原接口秒精度,避免 JSON 绑定阶段直接返回 400。
* 仅由 LeaveApplyDTO 的两个时间字段使用,不改变其他接口或响应的日期格式。
*/
public class LeaveApplyDateDeserializer extends JsonDeserializer<Date> {
// 可选的秒缺省为 00;严格解析防止 2 月 30 日、25 点等非法输入被自动调整成另一个时间。
private static final DateTimeFormatter FORMATTER = DateTimeFormatter
.ofPattern("uuuu-MM-dd HH:mm[:ss]").withResolverStyle(ResolverStyle.STRICT);
private static final ZoneId BUSINESS_ZONE = ZoneId.of("Asia/Shanghai");
@Override
public Date deserialize(JsonParser parser, DeserializationContext context) throws IOException {
// 保留 Jackson Date 原有的整数毫秒时间戳兼容性;字符串仍要求明确的日期时间格式。
if (parser.hasToken(JsonToken.VALUE_NUMBER_INT)) {
return new Date(parser.getLongValue());
}
if (!parser.hasToken(JsonToken.VALUE_STRING)) {
return (Date) context.handleUnexpectedToken(Date.class, parser);
}
String value = parser.getText().trim();
if (value.isEmpty()) {
// 缺失、null 和空字符串交给现有业务校验,统一提示“请假开始时间和结束时间不能为空”。
return null;
}
try {
return Date.from(LocalDateTime.parse(value, FORMATTER).atZone(BUSINESS_ZONE).toInstant());
} catch (DateTimeParseException exception) {
return (Date) context.handleWeirdStringValue(Date.class, value,
"请假时间格式应为 yyyy-MM-dd HH:mm 或 yyyy-MM-dd HH:mm:ss,且必须是有效日期时间");
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment