使用 Redis 缓存时获取类强制转换异常

getting class cast exception when using redis cache

提问人:let_there_be_peace 提问时间:10/24/2023 最后编辑:let_there_be_peace 更新时间:10/25/2023 访问量:54

问:

在处理应用程序时,我在服务层中添加了注释。我试图存储以反对。没有注释过程工作正常。但不是这样。spring boot@Cacheableorg.json.simple.JSONObjectjava.util.List<>@Cacheable

代码(服务)

@Cacheable(value = "EmployeeDetail")
    @Override
    public List<JSONObject> getAllRecords() {

    
        List<EmployeeDetail> list =  empService.fetchAll();
        List<JSONObject> arr=null;
        JSONObject obj = null;
        if(list!=null && list.size()>0) {
            arr = new ArrayList<JSONObject>();
            for(EmployeeDetail emp:list) {
                obj = new JSONObject();
                String ID = emp.getId()+"";
                String firstName = emp.getFirstName();
                String maidenName = emp.getMaidenName()==null?"":emp.getMaidenName();
                String lastName = emp.getLastName()==null?"":emp.getLastName();
                String FullName = firstName+" "+maidenName+" "+lastName;
                String Salary = emp.getSalary();
                String Address = emp.getAddress();
                String Gender = emp.getGender();
                obj.put("ID", ID);
                obj.put("Full Name", FullName);
                obj.put("Salary", Salary);
                obj.put("Address", Address);
                obj.put("Gender", Gender);
                
                arr.add(obj);
            }
        
        }
        return arr;
}

代码(控制器)

@GetMapping(value="getAll")
    public  List<JSONObject> fetch(){
        List<JSONObject> obj =  service.getAllRecords();
        return obj;

}

application.properties

server.port = 1122
spring.datasource.url=jdbc:mysql://localhost:3306/kitchoemployee?createDatabaseIfNotExist=true
spring.datasource.username=root
spring.datasource.password=root

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.show-sql=true
spring.jpa.hibernate.naming.implicit-strategy = org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
spring.jpa.hibernate.naming.physical-strategy = org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl


spring.cache.type=redis
spring.redis.host=localhost
spring.redis.port=6379

spring.cache.redis.key-prefix=emp-
#spring.cache.redis.time-to-live=60000
spring.cache.redis.use-key-prefix=true

我在响应中收到以下错误:

java.lang.ClassCastException: org.json.simple.JSONObject cannot be cast to java.util.List at com.kitcho.service.KitchoServiceImpl$$EnhancerBySpringCGLIB$$c9dae2fe.getAllRecords(<generated>) ~[classes!/:0.0.1-SNAPSHOT] at com.kitcho.controller.MyController.fetch(MyController.java:139) ~[classes!/:0.0.1-SNAPSHOT] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_212] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_212]

但是,当我发表评论时,注释代码运行良好。我之所以使用,是因为出于某些原因,UI 开发人员希望这种格式带有“全名”,中间有空格。@CacheableJsonObject

问题是什么。我该如何解决它。我想继续使用缓存。

java spring-boot spring-data-redis redis-cache

评论


答: 暂无答案