I'm using spring cache with redis implementation, I have following method

@Async
@Cacheable(key = "#id")
public Future<Student> getStudent(String id){
  Student stu  = ...;
  return new AsyncResult<>(stu);
}

When I visit the method first time, the data is cached into redis in json format.

but when I visit it the second time, it occurs some error like this:

java.util.concurrent.ExecutionException: org.springframework.data.redis.serializer.SerializationException: Could not read JSON: Cannot construct instance of org.springframework.scheduling.annotation.AsyncResult (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)

[EDIT]

I found a workaround : new a MyAsyncReslt.java which extends AsyncResult and add the NoArgsContructor.

1 Answers

1
Xarvalus On Best Solutions

Redis Serializer uses Jackson under the hood Class Jackson2JsonRedisSerializer, the error:

Cannot construct instance of org.springframework.scheduling.annotation.AsyncResult (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)

Seems to be outcoming from Jackson (can't deserialize without default constructor):

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of Type (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)

Ensure you have your model class Student properly structured to be compliant with Jackson Jackson - Object Serialization as it is used in AsyncResult via generics.


Edit in accordance to OP question:

I found a workaround : new a MyAsyncReslt.java which extends AsyncResult and add the NoArgsContructor.

Spring's AsyncResult seems not to be properly implemented to be serialized with Jackson (check on Github spring-projects/spring-framework: AsyncResult).

public class AsyncResult<V> implements ListenableFuture<V> {
    // ...

    public AsyncResult(@Nullable V value) {
        this(value, null);
    }

    private AsyncResult(@Nullable V value, @Nullable Throwable ex) {
        this.value = value;
        this.executionException = ex;
    }

    // Missing empty constructor to comply with Jackson requirements:
    public AsyncResult() {}

// ...

Until the issue will be patched you can extend Spring's AsyncResult and provide the required empty constructor. Freely use your custom AsyncResult in code.