While working on spring boot
application I added @Cacheable
annotation in service layer. I was trying to store org.json.simple.JSONObject
to java.util.List<>
object. Without the @Cacheable
annotation process is working fine. But not with it.
Code (service):
@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;
}
code (controller)
@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
I am getting below error in response:
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]
however when I comment the @Cacheable
annotation code runs fine.I am using JsonObject
because for some reasons, UI developers want this format with "Full Name" with space in between.
What is the issue. How can I resolve it. I want to keep using Caching.