使用redis缓存时出现类转换异常

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

在开发

spring boot
应用程序时,我在服务层添加了
@Cacheable
注释。我试图将
org.json.simple.JSONObject
存储到
java.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;

}

应用程序属性

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]

但是,当我评论时,

@Cacheable
注释代码运行良好。我使用
JsonObject
,因为由于某些原因,UI 开发人员希望这种格式带有“全名”,中间有空格。

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

java spring-boot spring-data-redis redis-cache
1个回答
0
投票

我希望这个方法可以帮助你解决你的问题。

更改缓存方法的返回类型:

如果您想继续使用

@Cacheable
,您可以将 getAllRecords 方法的返回类型更改为可以缓存的类型,例如
List<EmployeeDetail>
。然后,您可以在控制器或单独的方法中将
List<EmployeeDetail>
转换为
List<JSONObject>
,并缓存原始数据。从缓存检索数据并将其返回到 UI 时,您可以应用必要的
JSON
转换。

这是一个例子:

@Cacheable(value = "EmployeeDetail")
@Override
public List<EmployeeDetail> getAllRecords() {
    return empService.fetchAll();
}

@GetMapping(value = "getAll")
public List<JSONObject> fetch() {
    List<EmployeeDetail> employeeDetails = service.getAllRecords();
    List<JSONObject> jsonObjects = convertEmployeeDetailsToJSON(employeeDetails);
    return jsonObjects;
}

private List<JSONObject> convertEmployeeDetailsToJSON(List<EmployeeDetail> employeeDetails) {
    // Convert List<EmployeeDetail> to List<JSONObject> here
}

如果有效请告诉我。

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