parent
9234f26201
commit
b57d8d9add
18 changed files with 245 additions and 99 deletions
@ -0,0 +1,51 @@ |
||||
package org.springblade.desk.util.json.deserializer; |
||||
|
||||
import cn.hutool.core.date.DateTime; |
||||
import cn.hutool.core.date.DateUtil; |
||||
import com.fasterxml.jackson.core.JsonParser; |
||||
import com.fasterxml.jackson.databind.DeserializationContext; |
||||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer; |
||||
|
||||
import java.io.IOException; |
||||
import java.util.Date; |
||||
|
||||
/** |
||||
* 自定义Date反序列化器:将yyyy-MM-dd字符串转为当天开始时间(00:00:00)的Date对象 |
||||
*/ |
||||
public class DateBeginOfDayDeserializer extends StdDeserializer<Date> { |
||||
|
||||
// 固定日期格式
|
||||
private static final String DATE_PATTERN = "yyyy-MM-dd"; |
||||
|
||||
// 无参构造(Jackson要求)
|
||||
public DateBeginOfDayDeserializer() { |
||||
this(null); |
||||
} |
||||
|
||||
// 带类型构造
|
||||
public DateBeginOfDayDeserializer(Class<?> vc) { |
||||
super(vc); |
||||
} |
||||
|
||||
@Override |
||||
public Date deserialize(JsonParser jsonParser, DeserializationContext ctx) throws IOException { |
||||
// 1. 获取JSON中的字符串值(如:2026-01-01)
|
||||
String dateStr = jsonParser.getText().trim(); |
||||
|
||||
// 2. 空值处理(避免空指针)
|
||||
if (dateStr.isEmpty() || dateStr.equals("null")) { |
||||
return null; |
||||
} |
||||
try { |
||||
// 3. Hutool解析字符串为DateTime
|
||||
DateTime dateTime = DateUtil.parse(dateStr, DATE_PATTERN); |
||||
// 4. 转为当天开始时间(00:00:00)
|
||||
DateTime beginOfDay = DateUtil.beginOfDay(dateTime); |
||||
// 5. 转换为java.util.Date(适配Oracle DATE 7)
|
||||
return beginOfDay.toJdkDate(); |
||||
} catch (Exception e) { |
||||
// 6. 解析失败时抛出友好异常
|
||||
throw new IOException("日期格式错误,仅支持yyyy-MM-dd格式(如:2026-01-01)", e); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,52 @@ |
||||
package org.springblade.desk.util.json.deserializer; |
||||
|
||||
import cn.hutool.core.date.DateTime; |
||||
import cn.hutool.core.date.DateUtil; |
||||
import com.fasterxml.jackson.core.JsonParser; |
||||
import com.fasterxml.jackson.databind.DeserializationContext; |
||||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer; |
||||
|
||||
import java.io.IOException; |
||||
import java.util.Date; |
||||
|
||||
/** |
||||
* 自定义Date反序列化器:将yyyy-MM-dd字符串转为当天结束时间(23:59:59)的Date对象 |
||||
*/ |
||||
public class DateEndOfDayDeserializer extends StdDeserializer<Date> { |
||||
|
||||
// 固定日期格式
|
||||
private static final String DATE_PATTERN = "yyyy-MM-dd"; |
||||
|
||||
// 无参构造(Jackson要求)
|
||||
public DateEndOfDayDeserializer() { |
||||
this(null); |
||||
} |
||||
|
||||
// 带类型构造
|
||||
public DateEndOfDayDeserializer(Class<?> vc) { |
||||
super(vc); |
||||
} |
||||
|
||||
@Override |
||||
public Date deserialize(JsonParser jsonParser, DeserializationContext ctx) throws IOException { |
||||
// 1. 获取JSON中的字符串值(如:2026-01-01)
|
||||
String dateStr = jsonParser.getText().trim(); |
||||
|
||||
// 2. 空值处理(避免空指针)
|
||||
if (dateStr.isEmpty() || dateStr.equals("null")) { |
||||
return null; |
||||
} |
||||
|
||||
try { |
||||
// 3. Hutool解析字符串为DateTime
|
||||
DateTime dateTime = DateUtil.parse(dateStr, DATE_PATTERN); |
||||
// 4. 转为当天结束时间(23:59:59.999,不影响Oracle查询)
|
||||
DateTime endOfDay = DateUtil.endOfDay(dateTime); |
||||
// 5. 转换为java.util.Date(适配Oracle DATE 7)
|
||||
return endOfDay.toJdkDate(); |
||||
} catch (Exception e) { |
||||
// 6. 解析失败时抛出友好异常
|
||||
throw new IOException("日期格式错误,仅支持yyyy-MM-dd格式(如:2026-01-01)", e); |
||||
} |
||||
} |
||||
} |
||||
@ -1,4 +1,4 @@ |
||||
package org.springblade.desk.util.json; |
||||
package org.springblade.desk.util.json.serializer; |
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator; |
||||
import com.fasterxml.jackson.databind.SerializerProvider; |
||||
@ -1,4 +1,4 @@ |
||||
package org.springblade.desk.util.json; |
||||
package org.springblade.desk.util.json.serializer; |
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator; |
||||
import com.fasterxml.jackson.databind.SerializerProvider; |
||||
Loading…
Reference in new issue