I am using Redisson in my project to interact with Redis. I want to use org.redisson.codec.JsonJacksonCodec for serializing and deserializing my custom objects.
However, when I configure Redisson with this codec, I encounter issues when storing and retrieving data from Redis. Here is my configuration:
file redisson-config.yaml
:
singleServerConfig:
address: "redis://127.0.0.1:6379"
password: null
connectionMinimumIdleSize: 10
connectionPoolSize: 64
idleConnectionTimeout: 10000
connectTimeout: 10000
timeout: 3000
retryAttempts: 3
retryInterval: 1500
threads: 16
nettyThreads: 32
codec: !<org.redisson.codec.JsonJacksonCodec> {}
transportMode: "NIO"
file properties.yaml
:
spring:
jpa:
show-sql: true
hibernate:
ddl-auto: update
properties:
hibernate:
cache:
use_second_level_cache: true
use_query_cache: true
region:
factory_class: org.redisson.hibernate.RedissonRegionFactory
redisson:
config: ./redisson-config.yml
hibernate_2nd_cat_cache:
expiration:
time_to_live: 30000 # millisecond
my Cat
class look like this:
@Entity
@org.hibernate.annotations.Cache(region = "hibernate_2nd_cat_cache", usage = CacheConcurrencyStrategy.READ_WRITE)
@Table(name = "cat")
public class Cat implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Long id;
@Column(name = "name")
private String name;
@Column(name = "age")
private Integer age;
public Cat() {
}
...
Now, when running the program, I encounter an this error:
2024-12-07T17:01:51.671+07:00 ERROR 27892 --- [cat-service-demo] [nio-8080-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.springframework.orm.jpa.JpaSystemException: org.redisson.client.RedisException: Unexpected exception while processing command] with root cause
com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Could not resolve subtype of [simple type, class java.lang.Object]: missing type id property '@class'
at [Source: REDACTED (`StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION` disabled); line: 1, column: 263]
The data in Redis is not properly serialized, or I receive error when deserializing it.
So, what should I do to make JsonJacksonCodec
work correctly with my custom classes? Do I need additional configuration for Jackson or Redisson for that?
If there’s a better approach to handle custom object serialization/deserialization in Redisson, please let me know.
QuangBui is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.