spring web ResponseEntity可以';t系列化
如果使用@Cacheable作为返回值“ResponseEntity”,我得到了序列化错误spring web ResponseEntity可以';t系列化,spring,spring-mvc,spring-data,spring-data-redis,Spring,Spring Mvc,Spring Data,Spring Data Redis,如果使用@Cacheable作为返回值“ResponseEntity”,我得到了序列化错误 Caused by: org.springframework.data.redis.serializer.SerializationException: Cannot serialize; nested exception is org.springframework.core.serializer.support.SerializationFailedException: Failed to seria
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) {
//....
}
}
@控制器
@CacheConfig(cacheNames=“logs”)
公共类日志控制器{
@可缓存(key=“#id”)
@RequestMapping(value=LogConstants.LOGS\u ID\u PATH,method=RequestMethod.GET)
公共响应findById(@PathVariable Long id){
//....
}
}
缓存对象应该是可序列化的
,但响应性
不是可序列化的
您可以在不同级别上添加缓存,这样就可以使返回类型可序列化,或者添加一些客户序列化器/反序列化器,这些序列化器将能够保存
响应属性
您需要序列化响应属性,如:
public CustomeResponseEntity extends ResponseEntity implements Serializable {
private static final long serialVersionUID = 7156526077883281625L;
public CustomResponseEntity(HttpStatus status) {
super(status);
}
public CustomResponseEntity(Object body, HttpStatus status) {
super(body, status);
}
public CustomResponseEntity(MultiValueMap headers, HttpStatus status) {
super(headers, status);
}
public CustomResponseEntity(Object body, MultiValueMap headers, HttpStatus status) {
super(body, headers, status);
}
}
那就行了
return new CustomResponseEntity(resultDTO, HttpStatus.OK);
没有为我的工作。我的解决方案是创建一个组件作为中间人。这是不正确的-问题不是键不可序列化,而是方法返回类型不可序列化。@Madbreaks这确实是一个错误的答案,感谢您指出您的答案是错误的。是的,ResponseEntity是可序列化的,但它不可反序列化。原因是ResponseEntity没有无参数构造函数。但是对于反序列化,应该有一个有效的无参数构造函数。