Query That Returns Either a Result or null

Hi, I’m struggling with a case, which I thought would be an easy thing.

What I want to achieve is a query handler that either returns an instance of the type to be returned or null, of there is no instance.

Regardless, of whatever I try, in case of non existing instances (which would let the query handler return null), I get exceptions saying that org.axonframework.serialization.UnknownSerializedType can’t be casted to the desired type.

What is the best practice for query handlers that might have to indicate that a certain queried does not exist?

Thanks

Have you tried the ResponseType Optional? This should do exactly what you need.

So the queryHandler method returns Optional (via Optional.ofNullable(t)) and you query via queryGateway.query(query, ResponseTypes.optionalInstanceOf(T.class))

Thanks Jan, yes I think I tried that too… but I’m not sure (I tried a lot of different things). So I’ll try your suggestion and let you know.

1 Like