Fix: Spring Boot + Caching: DefaultSerializer requires a Serializable payload


ERROR 4033 --- [nio-8080-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : 

Servlet.service() for servlet [dispatcherServlet] in context with path [] 
threw exception [Request processing failed: org.springframework.data.redis.serializer.
SerializationException: Cannot serialize] with root cause

java.lang.IllegalArgumentException: 
DefaultSerializer requires a Serializable payload but received 
an object of type [com.example.redisdemo.DbUser]
	at org.springframework.core.serializer
           DefaultSerializer.serialize(DefaultSerializer.java:43)

If you are using Spring Boot + Data JPA and Caching, like Redis Cache, you will get the above exception if you do not serialize your Entity or POJO classes.


Fix/Solution:

    Your Entiry class needs to have implements Serializable with a serialVersionUID field.

    Example: Change this:

        @Entity
        public class DbUser  {
    
        public DbUser(int userId, String userName) {
            this.userId = userId;
            this.userName = userName;
        }

    To this,

    import java.io.Serializable;
    
    @Entity
        public class DbUser implements Serializable {
    
        private static final long serialVersionUID = 1L;
    
        public DbUser(int userId, String userName) {
            this.userId = userId;
            this.userName = userName;
        }
Fix the Serializable Error Spring Boot and Caching
-




Have Questions? Post them here!