Dreampie
Dreampie

Reputation: 1331

spring web ResponseEntity can't serialization

if use @Cacheable for return value 'ResponseEntity',I got serialization error.

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]

demo:

@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) {
   //....
  }
}

Upvotes: 3

Views: 14462

Answers (2)

Orest
Orest

Reputation: 6748

Cache objects should be Serializable but ResponseEntity is not Serializable.

You could add cache on the different level, so it would be possible to make return type serializable or add some customer serializer/deserializer which would be able to save ResponseEntity

Upvotes: 6

Jagadeeshwar Rao
Jagadeeshwar Rao

Reputation: 21

You need to Serialize your ResponseEntity like:

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);
    }
}

Then it will work.

return new CustomResponseEntity(resultDTO, HttpStatus.OK);

Upvotes: 0

Related Questions