ClassCastException:带有加载器“app”的未命名模块:Spring Data Redis

问题描述 投票:0回答:4

我正在为 Redis 和 Spring Boot 开发一个小型演示项目。但是对于以下 GET 端点,我得到 ClassCastException。-

@GetMapping("/all")
    public List<Product> getAllProducts(){
        return productRepository.findAll();
    }

    @GetMapping("/product/{id}")
    public Product finProduct(@PathVariable int id){
        return productRepository.findProductById(id);
    }

服务-

public List<Product> findAll() {
        List<Product> products=new ArrayList<>();
        redisTemplate.opsForHash().values(HASH_KEY).forEach(e->products.add((Product)e));
        return products;
    }

    public Product findProductById(int id) {

        return (Product) redisTemplate.opsForHash().get(HASH_KEY, id);
    }

我收到以下错误-

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'; com.ayushsingh.springdataredisdemo.entity.Product is in unnamed module of loader org.springframework.boot.devtools.restart.classloader.RestartClassLoader @18f69edd)

我参考了以下文章:https://medium.com/javarevisited/classcast-exception-when-using-redis-and-springboot-frameworks-in-conjunction-ea132dd0d7ea,但仍然遇到相同的错误。
豆类-

@Configuration
@EnableRedisRepositories
public class RedisConfig {

    // Connection Configuration
    @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;
    }

    // Redis template to acces Redis server
    @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(getClass().getClassLoader()));
        template.setValueSerializer(new JdkSerializationRedisSerializer(getClass().getClassLoader()));
        template.setEnableTransactionSupport(true);

        template.afterPropertiesSet();
        return template;
    }
}

应用程序属性-

spring.devtools.restart.enabled=false
server.port=8082

请帮忙!

java spring spring-boot classcastexception spring-data-redis
4个回答
6
投票

我遇到了同样的错误。唯一的方法是删除

spring-devtools
模块。看来是
ClassLoader
造成的。但我不知道如何解决。但删除这个模块确实有效。


0
投票

试试这个方法:

在调用 SpringApplication.run(…) 之前,您需要设置一个系统属性。例如:

public static void main(String[] args) {
System.setProperty("spring.devtools.restart.enabled", "false");
SpringApplication.run(MyApp.class, args);}

参考Spring文档


0
投票

如果您正在使用 spring devtools。禁用 devtools 它将起作用。


0
投票
public static void main(String[] args) {
System.setProperty("spring.devtools.restart.enabled", "false");
SpringApplication.run(MyApp.class, args);}

我没有使用 Spring Dev Tools。但是,错误仍然存在。

© www.soinside.com 2019 - 2024. All rights reserved.