如何在 Spring Data Redis 中序列化为 Java 对象

问题描述

这是我在@configuration 类中定义的 redis 模板,它采用 jedis Connection 工厂,我已将自定义类 Student 设置为 Redistemplate 中的值

    @Bean
    public Redistemplate<String,Student> template() {
        Redistemplate<String,Student> template = new Redistemplate<String,Student>();
        template.setConnectionFactory(connectionFactory());
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new JdkSerializationRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setEnableTransactionSupport(true);
        template.afterPropertiesSet();
        return template;
    }

这是我的学生班

@Entity
@Table(name = "student")
@Getter
@Setter
public class Student implements Serializable {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id",nullable = false,unique = true)
    private Integer id;
    
    @Column(name = "full_name",nullable = false)
    private String fullName;
    
    @Column(name = "email",unique = true)
    private String email;
    
    @Column(name = "created_on",nullable = false)
    private LocalDateTime createdOn;
    
    @Column(name = "updated_on",nullable = false)
    private LocalDateTime updatedOn;
    
    @PrePersist
    public void onCreate() {
        this.createdOn = LocalDateTime.Now();
        this.updatedOn = LocalDateTime.Now();
    }
    
    @PreUpdate
    public void onUpdate() {
        this.updatedOn = LocalDateTime.Now();
    }
}

这是我的控制器代码

    @GetMapping("/student/{id}")
    @Cacheable(key="#id",value = "student")
    public Student getStudentHandler(@PathVariable Integer id) {
        return studentService.getStudentById(id);
    }

我正在使用 redis-cli 以这种形式查看数据

127.0.0.1:6380> get student::1
"\xac\xed\x00\x05sr\x00 com.demo.school.entity.Student\xc7\x8f\xd4\xd3\x86S@\x9c\x02\x00\x05L\x00\tcreatedOnt\x00\x19Ljava/time/LocalDateTime;L\x00\x05emailt\x00\x12Ljava/lang/String;L\x00\bfullNameq\x00~\x00\x02L\x00\x02idt\x00\x13Ljava/lang/Integer;L\x00\tupdatedOnq\x00~\x00\x01xpsr\x00\rjava.time.Ser\x95]\x84\xba\x1b\"H\xb2\x0c\x00\x00xpw\x0e\x05\x00\x00\a\xe4\x0c\x1c\x0f!*\x1ejA@xt\x00\rabc@gmail.comt\x00\x03abcsr\x00\x11java.lang.Integer\x12\xe2\xa0\xa4\xf7\x81\x878\x02\x00\x01I\x00\x05valuexr\x00\x10java.lang.Number\x86\xac\x95\x1d\x0b\x94\xe0\x8b\x02\x00\x00xp\x00\x00\x00\x01sq\x00~\x00\x05w\x0e\x05\x00\x00\a\xe4\x0c\x1c\x0f!*\x1ej\x93Hx"

我使用 Postgresql 作为我的数据库和 redis 进行缓存 我怎样才能使它可读。

解决方法

这很可能不起作用,因为您使用的是基于 JDK 的序列化程序。

将您的 bean 定义更改为

@Bean
public RedisTemplate<String,Student> template() {
    RedisTemplate<String,Student> template = new RedisTemplate<String,Student>();
    template.setConnectionFactory(connectionFactory());
    template.setKeySerializer(new StringRedisSerializer());
    template.setHashKeySerializer(new StringRedisSerializer());
    template.setHashKeySerializer(new GenericJackson2JsonRedisSerializer());
    template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
    template.setEnableTransactionSupport(true);
    template.afterPropertiesSet();
    return template;
}

在多个机器上部署代码时不应使用 JDK 序列化程序。