spring web ResponseEntity无法序列化

Dre*_*pie 3 spring spring-mvc spring-data spring-data-redis

如果使用 @Cacheable 作为返回值“ResponseEntity”,我收到序列化错误。

\n\n
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]\n
Run Code Online (Sandbox Code Playgroud)\n\n

演示\xef\xbc\x9a

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

Ore*_*est 6

缓存对象应该是SerializableResponseEntity不是Serializable

您可以在不同级别添加缓存,因此可以使返回类型可序列化或添加一些客户序列化器/反序列化器,这将能够节省ResponseEntity