SpringBoot integrates Redis object serialization

Entity class

@[email protected]@[email protected]@[email protected](name = "user")public class User {    @Id    @GeneratedValue(strategy = GenerationType.IDENTITY)    @TableId(value = "id", type = IdType.AUTO)    @Column(name = "id", columnDefinition = "int")    private Integer id;    @Column(name = "name")    private String name;     @Column(name = "date")    private LocalDateTime date;}

When I save to redis, the save fails because there is no serialization, and the Serializable interface is implemented in the entity class

Can be saved successfully, but some unrecognized characters are seen in the management tool

Let's take a look at the source code again. In the RedissonAutoConfiguration class, there is a default redisTemplate and an @ConditionalOnMissingBean annotation, indicating that we can customize a redisTemplate.

@[email protected](    name = {"redisTemplate"})public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {    RedisTemplate<Object, Object> template = new RedisTemplate();    template.setConnectionFactory(redisConnectionFactory);    return template;}

Write a RedisConfig class, customize the redisTemplate configuration, and implement Jason serialization.

@[email protected] class RedisConfig extends CachingConfigurerSupport {     @Bean    RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();        redisTemplate.setConnectionFactory(redisConnectionFactory);        ObjectMapper objectMapper = new ObjectMapper();        //设置所有访问权限以及所有的实际类型都可序列化和反序列化        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);        objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL);         Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);        //设置值的序列化采用jackson2JsonRedisSerializer        //StringRedisSerializer        RedisSerializer<?> stringSerializer = new StringRedisSerializer();        redisTemplate.setKeySerializer(stringSerializer);        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);        redisTemplate.setHashKeySerializer(stringSerializer);        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);        redisTemplate.afterPropertiesSet();        return redisTemplate;    }}

Let's take a look at the storage format in redis. It is Jason's format, but the storage format of the date type is too complicated. Is there a simpler one? I have two methods.

The first is to add time format serialization configuration in RedisConfig

objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);objectMapper.registerModule(new JavaTimeModule());

The date is not so complicated, simple and clear.

The second type is to add two annotations to the field on the time type. These two annotations need to be quoted

<dependency>    <groupId>com.fasterxml.jackson.datatype</groupId>    <artifactId>jackson-datatype-jsr310</artifactId></dependency>
@JsonDeserialize(using = LocalDateTimeDeserializer.class)@JsonSerialize(using = LocalDateTimeSerializer.class)@Column(name = "date")private LocalDateTime date;

Take a look at the format in redis and separate the contents of the date with commas

Finally, if you want the date to be stored in the format you want, add a comment on the field

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")