251219_01_ENH : LenientLocalDateTime 커스텀 어노테이션 추가
This commit is contained in:
@@ -4,7 +4,7 @@ plugins {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//버전 변경은 여기서
|
//버전 변경은 여기서
|
||||||
def commonVer = "beta.1.0.53"
|
def commonVer = "beta.1.0.54"
|
||||||
|
|
||||||
group = 'com.kri.common'
|
group = 'com.kri.common'
|
||||||
version = commonVer
|
version = commonVer
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package com.kri.common.datetime;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
|
||||||
|
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||||
|
|
||||||
|
import java.lang.annotation.*;
|
||||||
|
|
||||||
|
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER})
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Documented
|
||||||
|
@JacksonAnnotationsInside
|
||||||
|
@JsonDeserialize(using = LenientLocalDateTimeDeserializer.class)
|
||||||
|
public @interface LenientLocalDateTime {
|
||||||
|
|
||||||
|
String[] patterns() default {
|
||||||
|
"yyyy-MM-dd",
|
||||||
|
"yyyy-MM-dd HH",
|
||||||
|
"yyyy-MM-dd HH:mm",
|
||||||
|
"yyyy-MM-dd HH:mm:ss",
|
||||||
|
"yyyy-MM-dd'T'HH:mm",
|
||||||
|
"yyyy-MM-dd'T'HH:mm:ss"
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -0,0 +1,87 @@
|
|||||||
|
package com.kri.common.datetime;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonParser;
|
||||||
|
import com.fasterxml.jackson.databind.BeanProperty;
|
||||||
|
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||||
|
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||||
|
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||||
|
import com.fasterxml.jackson.databind.deser.ContextualDeserializer;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
import java.time.format.DateTimeFormatterBuilder;
|
||||||
|
import java.time.format.DateTimeParseException;
|
||||||
|
import java.time.temporal.ChronoField;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
public class LenientLocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime>
|
||||||
|
implements ContextualDeserializer {
|
||||||
|
|
||||||
|
private final DateTimeFormatter[] formatters;
|
||||||
|
|
||||||
|
public LenientLocalDateTimeDeserializer() {
|
||||||
|
this(new String[]{
|
||||||
|
"yyyy-MM-dd",
|
||||||
|
"yyyy-MM-dd HH",
|
||||||
|
"yyyy-MM-dd HH:mm",
|
||||||
|
"yyyy-MM-dd HH:mm:ss",
|
||||||
|
"yyyy-MM-dd'T'HH:mm",
|
||||||
|
"yyyy-MM-dd'T'HH:mm:ss"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private LenientLocalDateTimeDeserializer(String[] patterns) {
|
||||||
|
this.formatters = Arrays.stream(patterns)
|
||||||
|
.map(LenientLocalDateTimeDeserializer::withDefaultTimeParts)
|
||||||
|
.toArray(DateTimeFormatter[]::new);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static DateTimeFormatter withDefaultTimeParts(String pattern) {
|
||||||
|
DateTimeFormatterBuilder b = new DateTimeFormatterBuilder()
|
||||||
|
.appendPattern(pattern);
|
||||||
|
|
||||||
|
// 초(ss)가 있는 패턴이면 ".밀리초(1~3자리)"를 옵션으로 허용
|
||||||
|
// 예: 2025-12-19T14:50:12.1 / .12 / .123
|
||||||
|
// (pattern 자체에 S가 들어간 경우는 중복 추가 방지)
|
||||||
|
if (pattern.contains("ss") && !pattern.contains("S")) {
|
||||||
|
b.optionalStart()
|
||||||
|
.appendFraction(ChronoField.NANO_OF_SECOND, 1, 3, true) // true => '.' 포함
|
||||||
|
.optionalEnd();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 패턴에 분/초가 없으면 0으로 채움 (날짜만 와도 LocalDateTime으로 만들 수 있게)
|
||||||
|
return b
|
||||||
|
.parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
|
||||||
|
.parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)
|
||||||
|
.parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
|
||||||
|
.parseDefaulting(ChronoField.NANO_OF_SECOND, 0)
|
||||||
|
.toFormatter();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
|
||||||
|
String s = DateTimeUtil.trimToNull(p.getText());
|
||||||
|
if (s == null) return null;
|
||||||
|
|
||||||
|
for (DateTimeFormatter f : formatters) {
|
||||||
|
try {
|
||||||
|
return LocalDateTime.parse(s, f);
|
||||||
|
} catch (DateTimeParseException ignored) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw ctxt.weirdStringException(s, LocalDateTime.class,
|
||||||
|
"Unsupported datetime format. Expected one of patterns defined in @LenientLocalDateTime");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty property)
|
||||||
|
throws JsonMappingException {
|
||||||
|
if (property == null) return this;
|
||||||
|
|
||||||
|
LenientLocalDateTime ann = property.getAnnotation(LenientLocalDateTime.class);
|
||||||
|
if (ann == null) ann = property.getContextAnnotation(LenientLocalDateTime.class);
|
||||||
|
|
||||||
|
return (ann == null) ? this : new LenientLocalDateTimeDeserializer(ann.patterns());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user