Skip to content

Commit de7130d

Browse files
authored
Merge pull request #121 from hocgin/develop
[新增功能](develop): 用户配置
2 parents 01cd521 + 2c08fe1 commit de7130d

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

spring-boot-web/web-spring-boot-autoconfigure/src/main/java/in/hocg/boot/web/autoconfiguration/jackson/SerializerConfiguration.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package in.hocg.boot.web.autoconfiguration.jackson;
22

3+
import in.hocg.boot.web.autoconfiguration.jackson.localdatetime.LocalDateTimeDeserializer;
34
import in.hocg.boot.web.autoconfiguration.jackson.xlong.LongDeserializer;
45
import in.hocg.boot.web.autoconfiguration.jackson.xlong.LongSerializer;
56
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
67
import org.springframework.context.annotation.Bean;
78
import org.springframework.context.annotation.Configuration;
89

10+
import java.time.LocalDateTime;
11+
912
/**
1013
* Created by hocgin on 2020/9/4
1114
* email: hocgin@gmail.com
@@ -19,7 +22,7 @@ public class SerializerConfiguration {
1922
public Jackson2ObjectMapperBuilderCustomizer jacksonCustomizer() {
2023
return builder -> {
2124
// builder.serializerByType(LocalDateTime.class, new LocalDateTimeSerializer());
22-
// builder.deserializerByType(LocalDateTime.class, new LocalDateTimeDeserializer());
25+
builder.deserializerByType(LocalDateTime.class, new LocalDateTimeDeserializer());
2326
builder.serializerByType(Long.class, new LongSerializer());
2427
builder.deserializerByType(Long.class, new LongDeserializer());
2528
};

spring-boot-web/web-spring-boot-autoconfigure/src/main/java/in/hocg/boot/web/autoconfiguration/jackson/localdatetime/LocalDateTimeDeserializer.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package in.hocg.boot.web.autoconfiguration.jackson.localdatetime;
22

3+
import cn.hutool.core.convert.Convert;
4+
import cn.hutool.core.util.NumberUtil;
35
import cn.hutool.core.util.StrUtil;
46
import com.fasterxml.jackson.core.JsonParser;
57
import com.fasterxml.jackson.databind.DeserializationContext;
@@ -21,7 +23,11 @@ public class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {
2123
@Override
2224
public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
2325
if (Objects.nonNull(p) && StrUtil.isNotBlank(p.getText())) {
24-
return LocalDateTime.ofInstant(Instant.ofEpochMilli(p.getLongValue()), ZoneOffset.of("+8"));
26+
String text = p.getText();
27+
if (NumberUtil.isLong(text)) {
28+
return LocalDateTime.ofInstant(Instant.ofEpochMilli(p.getLongValue()), ZoneOffset.of("+8"));
29+
}
30+
return Convert.toLocalDateTime(text);
2531
}
2632
return null;
2733
}

0 commit comments

Comments
 (0)