当前位置: 首页 > 知识库问答 >
问题:

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

太叔睿
2023-03-14
    @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);
    }
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"

共有1个答案

苗承
2023-03-14

它很可能不起作用,因为您使用的是基于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;
}

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

 类似资料:
  • 问题内容: 如何序列化未实现Serializable的对象?我不能将其标记为Serializable,因为该类来自第3方库。 问题答案: 您不能序列化未实现的类,但可以将其包装在可以实现的类中。为此,您应该在包装器类上实现和,以便可以以自定义方式序列化其对象。 首先,使您的非序列化字段。 在中,首先调用流以存储所有非瞬态字段,然后调用其他方法来序列化不可序列化对象的各个属性。 在中,首先调用流以读

  • 问题内容: 我想深入克隆一个列表。为此,我们有一种方法 所以现在要克隆我的列表,我应该先将其转换为可序列化的。是否可以将列表转换为可序列化列表? 问题答案: 已实施的所有标准实施。 因此,即使它本身不是的子类型,也可以安全地将列表强制转换为,只要您知道它是诸如或的标准实现之一。 如果不确定,请先复制列表(使用),然后知道它是可序列化的。

  • 我使用下面的方法(来自spark-java framework的方法)从DB返回的数据如下: 从DB返回的数据: ClasStypeAdapterFactory: 类类型适配器: 这里我使用的是,它从spark-java ResponseTransformer接口实现了

  • 我有一个kdtree,其节点由以下字段组成:公共静态类节点实现可序列化{ 其中DataPoint定义: 公共静态类DataPoint实现可序列化{公共可比X;公共可比Y;公共可比Z; 我想序列化树,存储在文件中并在回答范围查询时反序列化。我对这个概念od序列化的理解并不好。从我收集的任何内容中,我编写了以下函数,但不起作用。有人能帮忙吗?

  • 我在爪哇是新来的! 我的目标:我有收集的对象,让我保持任何类型的对象通过一些键(如地图)(把他们到它和从)。我可以向它添加任何类型的对象(例如,我可以添加MyClass的对象)。然后,我使用GSON序列化这个对象以JSON格式分离字符串(例如,我可以序列化这个字符串以分离文件)。当我想拿回某个对象时,我通过键从collection请求它,collection将对应的字符串反序列化到object对象

  • 本文向大家介绍我们如何在Java中使用flexjson序列化对象列表?,包括了我们如何在Java中使用flexjson序列化对象列表?的使用技巧和注意事项,需要的朋友参考一下 该Flexjson 是一个轻量级的库序列化 和反序列化 Java对象到 和从 JSON 格式。我们可以使用JSONSerializer 类的serialize()方法来序列化 对象列表。此方法可以对目标实例执行浅 序列化。我