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

问题描述 投票:0回答:1

这是我在@configuration类中定义的redis模板,它采用jedis连接工厂,我已将自定义类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", nullable = false, 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\[email protected]\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 进行缓存 我怎样才能使它可读。

spring-boot redis spring-data-redis redis-cache
1个回答
0
投票

它很可能不起作用,因为您使用的是基于 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.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
    template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
    template.setEnableTransactionSupport(true);
    template.afterPropertiesSet();
    return template;
}

当您在多个盒子上部署代码时,不应使用 JDK 序列化程序。

© www.soinside.com 2019 - 2024. All rights reserved.