behlHardik
behlHardik

Reputation: 21

How to Serialize to Java Object in Spring Data Redis

This is my redis Template that i have defined in my @configuration class, it's taking a jedis Connection factory, i have set my custom class Student as the value in 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;
    }

This Is My Student Class

@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();
    }
}

Here is My Controller code

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

I am seeing data in this form using 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"

I am Using Postgresql as my database and redis for caching How Can i make this readable.

Upvotes: 0

Views: 8562

Answers (1)

sonus21
sonus21

Reputation: 5388

It's most likely not working because you're using JDK based serializer.

Change your bean definition to

@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 serializer should not be used when you're deploying your code on multiple box.

Upvotes: 1

Related Questions