|
| 1 | +Reactive Streams Wrapper |
| 2 | +--- |
| 3 | + |
| 4 | +This module wraps Feign's http requests in a [Reactive Streams](https://reactive-streams.org) |
| 5 | +Publisher, enabling the use of Reactive Stream `Publisher` return types. Supported Reactive Streams implementations are: |
| 6 | + |
| 7 | +* [Reactor](https://project-reactor.org) (`Mono` and `Flux`) |
| 8 | +* [ReactiveX (RxJava)](https://reactivex.io) (`Flowable` only) |
| 9 | + |
| 10 | +To use these wrappers, add the `feign-reactive-wrappers` module, and your desired `reactive-streams` |
| 11 | +implementation to your classpath. Then configure Feign to use the reactive streams wrappers. |
| 12 | + |
| 13 | +```java |
| 14 | +public interface GitHubReactor { |
| 15 | + |
| 16 | + @RequestLine("GET /repos/{owner}/{repo}/contributors") |
| 17 | + Flux<Contributor> contributors(@Param("owner") String owner, @Param("repo") String repo); |
| 18 | + |
| 19 | + class Contributor { |
| 20 | + String login; |
| 21 | + |
| 22 | + public Contributor(String login) { |
| 23 | + this.login = login; |
| 24 | + } |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +public class ExampleReactor { |
| 29 | + public static void main(String args[]) { |
| 30 | + GitHubReactor gitHub = ReactorFeign.builder() |
| 31 | + .target(GitHubReactor.class, "https://api.github.com"); |
| 32 | + |
| 33 | + List<Contributor> contributors = gitHub.contributors("OpenFeign", "feign") |
| 34 | + .map(Contributor::new) |
| 35 | + .collect(Collectors.toList()) |
| 36 | + .block(); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +public interface GitHubReactiveX { |
| 41 | + |
| 42 | + @RequestLine("GET /repos/{owner}/{repo}/contributors") |
| 43 | + Flowable<Contributor> contributors(@Param("owner") String owner, @Param("repo") String repo); |
| 44 | + |
| 45 | + class Contributor { |
| 46 | + String login; |
| 47 | + |
| 48 | + public Contributor(String login) { |
| 49 | + this.login = login; |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +public class ExampleRxJava2 { |
| 55 | + public static void main(String args[]) { |
| 56 | + GitHubReactiveX gitHub = RxJavaFeign.builder() |
| 57 | + .target(GitHub.class, "https://api.github.com"); |
| 58 | + |
| 59 | + List<Contributor> contributors = gitHub.contributors("OpenFeign", "feign") |
| 60 | + .map(Contributor::new) |
| 61 | + .collect(Collectors.toList()) |
| 62 | + .block(); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +``` |
| 67 | + |
| 68 | +Considerations |
| 69 | +--- |
| 70 | + |
| 71 | +These wrappers are not *reactive all the way down*, given that Feign generated requests are |
| 72 | +synchronous. Requests still block, but execution is controlled by the `Publisher` and their |
| 73 | +related `Scheduler`. While this may not be ideal in terms of a fully reactive application, providing these |
| 74 | +wrappers provide an intermediate upgrade path for Feign. |
| 75 | + |
| 76 | +### Streaming |
| 77 | + |
| 78 | +Methods that return `java.util.streams` Types are not supported. Responses are read fully, |
| 79 | +the wrapped in the appropriate reactive wrappers. |
| 80 | + |
| 81 | +### Iterable and Collections responses |
| 82 | + |
| 83 | +Due to the Synchronous nature of Feign requests, methods that return `Iterable` types must specify the collection |
| 84 | +in the `Publisher`. For `Reactor` types, this limits the use of `Flux` as a response type. If you |
| 85 | +want to use `Flux`, you will need to manually convert the `Mono` or `Iterable` response types into |
| 86 | +`Flux` using the `fromIterable` method. |
| 87 | + |
| 88 | + |
| 89 | +```java |
| 90 | +public interface GitHub { |
| 91 | + |
| 92 | + @RequestLine("GET /repos/{owner}/{repo}/contributors") |
| 93 | + Mono<List<Contributor>> contributors(@Param("owner") String owner, @Param("repo") String repo); |
| 94 | + |
| 95 | + class Contributor { |
| 96 | + String login; |
| 97 | + |
| 98 | + public Contributor(String login) { |
| 99 | + this.login = login; |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +public class ExampleApplication { |
| 105 | + public static void main(String[] args) { |
| 106 | + GitHub gitHub = ReactorFeign.builder() |
| 107 | + .target(GitHub.class, "https://api.github.com"); |
| 108 | + |
| 109 | + Mono<List<Contributor>> contributors = gitHub.contributors("OpenFeign", "feign"); |
| 110 | + Flux<Contributor> contributorFlux = Flux.fromIterable(contributors.block()); |
| 111 | + } |
| 112 | +} |
| 113 | +``` |
0 commit comments