Commit fea1033a by HlQ

[chg] 日期时间序列化、反序列化调整

1 parent 560dd3c6
package vion.config; package vion.config;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer; import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer; import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
...@@ -16,13 +20,18 @@ import java.time.format.DateTimeFormatter; ...@@ -16,13 +20,18 @@ import java.time.format.DateTimeFormatter;
public class JacksonConfig { public class JacksonConfig {
private static final String dateTimeFormat = "yyyy-MM-dd HH:mm:ss"; private static final String dateTimeFormat = "yyyy-MM-dd HH:mm:ss";
private static final String dateFormat = "yyyy-MM-dd";
private static final String timeFormat = "HH:mm:ss"; private static final String timeFormat = "HH:mm:ss";
@Bean @Bean
public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() { public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
return builder -> { return builder -> {
builder.serializers(new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(dateTimeFormat))); builder.serializers(new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(dateTimeFormat)));
builder.serializers(new LocalDateSerializer(DateTimeFormatter.ofPattern(dateFormat)));
builder.serializers(new LocalTimeSerializer(DateTimeFormatter.ofPattern(timeFormat))); builder.serializers(new LocalTimeSerializer(DateTimeFormatter.ofPattern(timeFormat)));
builder.deserializers(new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(dateTimeFormat)));
builder.deserializers(new LocalDateDeserializer(DateTimeFormatter.ofPattern(dateFormat)));
builder.deserializers(new LocalTimeDeserializer(DateTimeFormatter.ofPattern(timeFormat)));
}; };
} }
} }
...@@ -2,6 +2,12 @@ package vion.config; ...@@ -2,6 +2,12 @@ package vion.config;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import org.redisson.Redisson; import org.redisson.Redisson;
import org.redisson.api.RedissonClient; import org.redisson.api.RedissonClient;
import org.redisson.codec.JsonJacksonCodec; import org.redisson.codec.JsonJacksonCodec;
...@@ -11,6 +17,11 @@ import org.springframework.beans.factory.annotation.Value; ...@@ -11,6 +17,11 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
/** /**
* @author HlQ * @author HlQ
* @date 2023/12/19 * @date 2023/12/19
...@@ -27,11 +38,22 @@ public class RedisConfig { ...@@ -27,11 +38,22 @@ public class RedisConfig {
@Value("${spring.data.redis.database}") @Value("${spring.data.redis.database}")
private Integer database; private Integer database;
private static final String dateTimeFormat = "yyyy-MM-dd HH:mm:ss";
private static final String dateFormat = "yyyy-MM-dd";
private static final String timeFormat = "HH:mm:ss";
@Bean(destroyMethod = "shutdown") @Bean(destroyMethod = "shutdown")
public RedissonClient redissonClient() { public RedissonClient redissonClient() {
Config config = new Config(); Config config = new Config();
ObjectMapper objectMapper = new ObjectMapper(); ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule()); var module = new JavaTimeModule();
module.addSerializer(new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(dateTimeFormat)));
module.addSerializer(new LocalDateSerializer(DateTimeFormatter.ofPattern(dateFormat)));
module.addSerializer(new LocalTimeSerializer(DateTimeFormatter.ofPattern(timeFormat)));
module.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(dateTimeFormat)));
module.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(dateFormat)));
module.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(timeFormat)));
objectMapper.registerModule(module);
config.setCodec(new JsonJacksonCodec(objectMapper)); config.setCodec(new JsonJacksonCodec(objectMapper));
SingleServerConfig singleServerConfig = config.useSingleServer(); SingleServerConfig singleServerConfig = config.useSingleServer();
singleServerConfig.setAddress("redis://" + host + ":" + port) singleServerConfig.setAddress("redis://" + host + ":" + port)
......
...@@ -5,9 +5,7 @@ import lombok.Getter; ...@@ -5,9 +5,7 @@ import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import vion.dto.BaseDTO; import vion.dto.BaseDTO;
import java.time.LocalDate;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.LocalTime;
@Getter @Getter
@Setter @Setter
...@@ -41,13 +39,4 @@ public class Account extends BaseDTO { ...@@ -41,13 +39,4 @@ public class Account extends BaseDTO {
* 备注 * 备注
*/ */
private String remark; private String remark;
@TableField(exist = false)
private LocalDateTime dt;
@TableField(exist = false)
private LocalDate d;
@TableField(exist = false)
private LocalTime t;
} }
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!