【发布时间】:2022-10-22 09:11:02
【问题描述】:
我有这个缓存,它基本上是一个 DTO 列表:
A类:
@Cacheable("MyList")
public List<MyObjectDTO> setCachedList(){
return api.getList(); //call to another api to fetch that list
}
B类:
public List<MyObjectDTO> getCachedList(){
if(!CacheManager.getCacheNames().contains("MyList"){
ClassB.setCachedList();
}
Cache cache = CacheManager.getCache("MyList");
Type listType = new TypeToken<List<MyObjectDTO>>(){}.getType(); //error occurs here
List<MyObjectDTO> returnList = modelMapper.map(cache, listType);
return returnList;
我在上面的代码中收到以下错误:
Failed to instantiate instance of destination java.util.List. Ensure that java.util.List has a non-private no-argument constructor.
我看到了一个类似的问题,但由于我使用的是列表缓存,所以我无法将其扩展到具体类。
更新我能够通过在 modelMapper 而不是缓存中传递 cache.get(SimpleKey.EMPTY).get() 来解决这个问题。
【问题讨论】:
标签: java caching modelmapper