gpt4 book ai didi

spring web ResponseEntity 无法序列化

转载 作者:行者123 更新时间:2023-12-02 21:08:15 34 4
gpt4 key购买 nike

如果使用@Cacheable 作为返回值'ResponseEntity',我会遇到序列化错误。

Caused by: org.springframework.data.redis.serializer.SerializationException: Cannot serialize; nested exception is org.springframework.core.serializer.support.SerializationFailedException: Failed to serialize object using DefaultSerializer; nested exception is java.lang.IllegalArgumentException: DefaultSerializer requires a Serializable payload but received an object of type [org.springframework.http.ResponseEntity]

演示:

@Controller
@CacheConfig(cacheNames = "logs")
public class LogController {
@Cacheable(key = "#id")
@RequestMapping(value = LogConstants.LOGS_ID_PATH, method = RequestMethod.GET)
public ResponseEntity<Log> findById(@PathVariable Long id) {
//....
}
}

最佳答案

缓存对象应该是 SerializableResponseEntity 不是 Serializable

您可以在不同级别添加缓存,这样就可以使返回类型可序列化或添加一些能够保存 ResponseEntity

的客户序列化器/反序列化器

关于spring web ResponseEntity 无法序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34984786/

34 4 0