SpringBoot中緩存@Cacheable出錯(cuò)的問題解決
錯(cuò)誤代碼:
@Cacheable(value = "FrontAdvertiseVOList", keyGenerator = "cacheKey")
@Override
public List<FrontAdvertiseVO> getFrontAdvertiseVOList(Integer count) {
return this.list(Wrappers.<Advertise>lambdaQuery()
.select(Advertise::getPic, Advertise::getUrl)
.eq(Advertise::getState, 1)
.orderByDesc(Advertise::getPriority)
.last("limit " + count))
.stream()
.map(advertise -> new FrontAdvertiseVO(advertise.getPic(), advertise.getUrl()))
.toList(); // ----------- ①
}
運(yùn)行程序,出錯(cuò):
org.springframework.data.redis.serializer.SerializationException: Could not read JSON:Unexpected token (START_OBJECT), expected VALUE_STRING: need String, Number of Boolean value that contains type id (for subtype of java.lang.Object) at [Source: REDACTED (`StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION` disabled); line: 1, column: 2]
解決方案
只需要將編號①處的代碼修改為:
.collect(Collectors.toList());
原理
collect(Collectors.toList())返回的數(shù)據(jù):

toList()返回的數(shù)據(jù):

collect(Collectors.toList()) 和 toList() 的主要區(qū)別在于返回的列表類型和可變性:
- collect(Collectors.toList()):返回的是一個(gè)普通的 ArrayList ,因此可以進(jìn)行添加、刪除和修改操作
- toList():返回的是通過對原始數(shù)組創(chuàng)建一個(gè)不可修改的列表。一旦創(chuàng)建,就不能對其進(jìn)行添加、刪除或修改操作
使用場景:
- toList():適用于不需要對列表進(jìn)行修改的場景,如從數(shù)據(jù)庫查詢數(shù)據(jù)等,因?yàn)樗祷氐氖遣豢勺兞斜?,可以防止?shù)據(jù)被意外修改
- collect(Collectors.toList()):適用于需要對列表進(jìn)行修改的場景,因?yàn)樗祷氐氖瞧胀ǖ腁rrayList,可以進(jìn)行各種操作
到此這篇關(guān)于SpringBoot中緩存@Cacheable出錯(cuò)的問題解決的文章就介紹到這了,更多相關(guān)SpringBoot緩存@Cacheable出錯(cuò)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringMVC中MultipartFile轉(zhuǎn)File的兩種方式
在spring上傳文件中,一般都使用了MultipartFile來接收,但是有需要用到File的地方,本文主要介紹了SpringMVC中MultipartFile轉(zhuǎn)File的兩種方式,感興趣的可以了解一下2022-04-04
Spring Boot實(shí)現(xiàn)郵件注冊功能示例代碼
本篇文章主要介紹了Spring Boot實(shí)現(xiàn)郵件注冊功能示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05
MybatisPlusInterceptor依賴變紅如何解決,無法識別問題
這篇文章主要介紹了MybatisPlusInterceptor依賴變紅如何解決,無法識別問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
淺談@RequestParam(required = true)的誤區(qū)
這篇文章主要介紹了@RequestParam(required = true)的誤區(qū),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
springboot+thymeleaf+druid+mybatis 多模塊實(shí)現(xiàn)用戶登錄功能
這篇文章主要介紹了springboot+thymeleaf+druid+mybatis 多模塊實(shí)現(xiàn)用戶登錄功能,本文通過示例代碼圖文相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
深入理解springboot中配置文件application.properties
本文主要介紹了springboot中配置文件application.properties,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
關(guān)于WeakhashMap與HashMap之間的區(qū)別和聯(lián)系
這篇文章主要介紹了關(guān)于WeakhashMap與HashMap之間的區(qū)別和聯(lián)系,WeakHashMap從名字可以得知主要和Map有關(guān),不過還有一個(gè)Weak,我們就更能自然而然的想到這里面還牽扯到一種弱引用結(jié)構(gòu),因此想要徹底搞懂,我們還需要知道四種引用,需要的朋友可以參考下2023-09-09

