Missing an OptionalResponseType for query that return Optional<R>
See original GitHub issueHi all,
I’ve just tested the last version of Axon framework (3.2) and I ran into an issues with queries that return an Optional:
queryGateway.query(query, Optional.class)
Axon still complains that it can’t find a handler for my optional, despite I have a @QueryHandler
for my query.
It would be great to have an OptionalResponseType
like MultipleInstancesResponseType
that can handle Optionals.
I’ve try something like this:
public class OptionalResponseType<R> extends AbstractResponseType<Optional<R>> {
private OptionalResponseType(Class<?> expectedResponseType) {
super(expectedResponseType);
}
@Override
public boolean matches(Type responseType) {
return isParameterizedType(responseType) &&
isParameterizedTypeOfExpectedType(responseType) &&
isOptional(responseType);
}
@SuppressWarnings("unchecked")
private boolean isOptional(Type responseType) {
return ((Class)((ParameterizedType) responseType).getRawType()).isAssignableFrom(Optional.class);
}
@Override
@SuppressWarnings("unchecked")
public Optional<R> convert(Object response) {
if (response instanceof Optional) {
return super.convert(response);
} else {
return Optional.ofNullable((R) response);
}
}
public static <R> ResponseType<Optional<R>> optionalInstancesOf(Class<R> type) {
return new OptionalResponseType<>(type);
}
}
It seems to work but it would be cool to have it in org.axonframework.queryhandling.responsetypes.ResponseTypes
Regards, Sylvain
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Java Optional as Return Type - Baeldung
The Optional type was introduced in Java 8. It provides a clear and explicit way to convey the message that there may not...
Read more >Spring Data JPA findOne() change to Optional how to use this?
This method is a query by example search and you don't want that as a replacement. In fact, the method with the same...
Read more >How to model an optional response field? - Google Groups
I have one of my endpoint returns a paginated list of documents. Each page of results contains URL for next/prev/first/last page of output ......
Read more >Using Optional and Nullable Properties in API Requests
Learn how optional and nullable properties can be used flexibly in combinations in each parameter of your API requests made from a SDK....
Read more >Chapter 10. Using Optional as a better alternative to null
So far, so good; you've learned how to employ optionals in types to clarify your domain model and the advantages this offers over...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks for sharing that @thomaslhostis, but there currently is an
OptionalResponseType
implementation in Axon Framework you can use for this scenario. Here is the implementation, which has been introduced in Axon 4.2.Hence upgrading to a more recent version (currently this is 4.4.3) will allow you to use it.
@FabianTe This is how @Sylvain-maillard implemented it using version 3.4 of Axon : https://github.com/voyages-sncf-technologies/hesperides/blob/master/commons/src/main/java/org/hesperides/commons/axon/AxonResponseType.java
And how we use it: https://github.com/voyages-sncf-technologies/hesperides/blob/267f30da8c6bc2b42e309fcbce3293c99366d1cb/commons/src/main/java/org/hesperides/commons/axon/AxonQueries.java#L49