[Question] Optional with CompletableFuture?
See original GitHub issueHello,
I am using retrofit 2.5.0 and have an API like:
CompletableFuture<Optional<String>> getCustomer(int customerId)
But the response is null instead of Optional.empty().
Here’s my retrofit builder:
new Retrofit.Builder()
.baseUrl(getBaseUrl())
.addConverterFactory(new NullOnEmptyConverterFactory())
.client(getOkHttpClient(getOAuthInterceptor(decrypter())))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(create(objectMapper))
.build();
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
From Optional<CompletableFuture<T>> to ... - Stack Overflow
Show activity on this post. I would like to convert Optional<CompletableFuture<T>> to CompletableFuture<Optional<T>> .
Read more >CompletableFuture with Optional : r/javahelp - Reddit
So I'd think you'd check the optional and throw an exception if it's an empty. I'm assuming exceptions work in lambdas though. I'm...
Read more >Optional in Java 8: Part 2 - Educative.io
This method returns the value present in the optional. If no value is present, then it throws the exception created by the provided...
Read more >Write Clean Asynchronous Code With CompletableFuture ...
CompletableFuture allows us to write non-blocking code by running a task on a separate thread than the main application thread and notifying the ......
Read more >20 Practical Examples: Java's CompletableFuture - DZone
In addition to implementing the CompletionStage interface, CompletableFuture also implements Future , which represents a pending asynchronous ...
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 Free
Top 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

NullOnEmptyis for broken servers who return 0-length bodies instead of JSON literals ofnullfor an absent body.Right now there’s not a good way to differentiate this case because the HTTP status code bypasses the converter. If you don’t care about distinguishing between 204 and 205 you can use an OkHttp interceptor to rewrite them to 200’s with a
nullliteral as the root element.Beyond that, we probably need to add a method to
Converter.Factoryfor handling HTTP 204/205 since there is no body but the user requested an instance of a particular type. I need to think about it more.Great. I might have a question later but thank you for the explanation.