question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Missing an OptionalResponseType for query that return Optional<R>

See original GitHub issue

Hi 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:closed
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
smcvbcommented, Oct 9, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found