Rrue
Rrue

Reputation: 31

Failed to serialize 'org.springframework.http.ResponseEntity' HazelcastCache

I am using Hazelcast for cache,

My Controller looks like this one:

public ResponseEntity find(String user) {

.....

return ResponseEntity.ok(new ResponseDto(list, null));

}

When the Hazelcast is trying to save in cache: Exception: Failed to serialize 'org.springframework.http.ResponseEntity

ResponseEntity is not serializable

Any idea? Thanks in advance.

Upvotes: 1

Views: 2243

Answers (2)

instanceof
instanceof

Reputation: 1466

What I ended up doing was split my code into two methods, one that returns the ResponseEntity object, and one that returns the input to the ResponseEntity object, which in your case appears to be your ResponseDto object. I then adapted that custom class to implement Serializable, so that it can be cached by Hazelcast. Then lastly I put the @Cacheable annotation above the method that returns that object, instead of the method that returns ResponseEntity. In doing so, I no longer received that error.

Upvotes: 0

Nicolas
Nicolas

Reputation: 1186

To answer you question, you have basically 2 options:

  • Change the serialization strategy to something that doesn't need to change the class hierarchy (since you cannot) e.g. JSON serialization
  • Think whether you want to serialize the ResponseEntity, or more likely the underlying DTO that you can make Serializable

But the thing is, I don't understand how you cache, or from where, or even why. In order to help you in the best way possible, please remember to define the context first, then state the problem you try to solve by caching, and only then can you describe what how you implemented caching and the exception.

For example, in your question, I infer you're using Spring, but I don't know which version, if it's Spring Boot or not, what dependencies you have, etc.

Upvotes: -1

Related Questions