I am trying to use Redis with Spring Boot. I am facing error as described- Steps- 1. I created a Spring Boot Application with following dependencies-
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>5.0.0-alpha1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
- I have added the following Configuration for Redis-
@Bean
public JedisConnectionFactory connectionFactory() {
RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
configuration.setHostName("localhost");
configuration.setPort(6379);
Duration readTimeout = Duration.ofMillis(30 * 1000);
Duration connectTimeout = Duration.ofMillis(3 * 1000);
JedisClientConfiguration clientConfiguration = JedisClientConfiguration.builder()
.readTimeout(readTimeout)
.connectTimeout(connectTimeout)
.usePooling()
.build();
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(configuration, clientConfiguration);
return jedisConnectionFactory;
}
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory());
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new JdkSerializationRedisSerializer());
template.setValueSerializer(new JdkSerializationRedisSerializer(getClass().getClassLoader()));
template.setEnableTransactionSupport(true);
template.afterPropertiesSet();
return template;
}
- Entity-
@Data
@AllArgsConstructor
@NoArgsConstructor
@RedisHash("Product")
public class Product implements Serializable{
@Id
private int id;
private String name;
private int qty;
long price;
}
- Created some rest endpoints-
@RestController
@RequestMapping("/redisdemo")
public class ProductController {
@Autowired
ProductRepository productRepository;
@PostMapping("/save")
public Product save(@RequestBody Product product){
return productRepository.saveProduct(product);
}
@DeleteMapping("/delete/{id}")
public String delete(@PathVariable int id){
return productRepository.deleteProduct(id);
}
@GetMapping("/all")
public List<Product> getAllProducts(){
return productRepository.findAll();
}
@GetMapping("/product/{id}")
public Product finProduct(@PathVariable int id){
return productRepository.findProductById(id);
}
}
Working- 1. /save- To save the entity (working as expected) 2. /delete- To delete entity (working as expected) 3. /all- List all the entities (ClassCastException) 4. /product/{id}- List the entity by id (ClassCastException) The exception is-
java.lang.ClassCastException: class com.ayushsingh.springdataredisdemo.entity.Product cannot be cast to class com.ayushsingh.springdataredisdemo.entity.Product (com.ayushsingh.springdataredisdemo.entity.Product is in unnamed module of loader 'app';
Also posted on Stackoverflow- https://stackoverflow.com/questions/75943457/classcastexception-with-unnamed-module-of-loader-app-spring-data-redis
Comment From: philwebb
Lets keep the discussion on stackoverflow.