ClassCastException for same class with Spring Boot + Redis Cache

Asked on August 12, 2018
Hi, I am creating Spring Boot application with Redis Cache. My pom.xml is
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
I am running application using command
mvn spring-boot:run
Once data is cached then in second hit we are not getting cached data instead an error.
java.lang.ClassCastException: com.test.entity.Student cannot be cast to com.test.entity.Student
How to fix it?

Replied on August 12, 2018
The error is because of Spring Boot Dev Tools limitations. According to Spring Boot Doc
"Restart functionality does not work well with objects that are deserialized by using a standard ObjectInputStream."
As you are using
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>That is why you are getting ClassCastException. Work around are
1. Run application using executable JAR
Create executable Jar using
mvn clean package
Then that JAR
java -jar target/myapp.jar
2. Disable caching for testing purpose.
3. Handle serialization/deserialization.

Replied on August 12, 2018
Ok. Thanks.