Spring 5 reactive - 如何在 Kotlin 中使用 Flux.zip
Spring 5 reactive - How to use Flux.zip in Kotlin
我有这个 Java 代码:
WebClient webClient = WebClient.create("http://localhost:8080/api/v1");
Flux<Game> games = webClient.get().uri("/games").retrieve().bodyToFlux(Game.class);
Flux<Player> players = webClient.get().uri("/players").retrieve().bodyToFlux(Player.class);
Flux.zip(games, players, (g, p) -> new Tournament(new ArrayList<>(), new ArrayList<>()));
如何将其翻译成 Kotlin?
val webClient = WebClient.create("http://localhost:8080/api/v1")
val games = webClient.get().uri("/games").retrieve().bodyToFlux(Game::class.java)
val players = webClient.get().uri("/players").retrieve().bodyToFlux(Player::class.java)
Flux.zip<Game, Player, Tournament>(games, players) { g, p -> Tournament(emptyList(), emptyList()) }
以上无效,zip 方法不喜欢它的参数:
Error:(24, 14) Kotlin: None of the following functions can be called with the arguments supplied:
@SafeVarargs public final fun <I : Any!, O : Any!> zip(p0: (Array<(out) Any!>!) -> Player!, p1: Int, p2: Array<(out) Publisher<out Game!>!>): Flux<Player!> defined in reactor.core.publisher.Flux
public final fun <T1 : Any!, T2 : Any!, O : Any!> zip(p0: (Subscriber<in Game!>!) -> Unit, p1: (Subscriber<in Player!>!) -> Unit, p2: (Game!, Player!) -> Tournament!): Flux<Tournament!> defined in reactor.core.publisher.Flux
public final fun <T1 : Any!, T2 : Any!, T3 : Any!> zip(p0: (Subscriber<in Game!>!) -> Unit, p1: (Subscriber<in Player!>!) -> Unit, p2: (Subscriber<in Tournament!>!) -> Unit): Flux<Tuple3<Game!, Player!, Tournament!>!> defined in reactor.core.publisher.Flux
public final fun <O : Any!> zip(p0: (Mutable)Iterable<Publisher<*>!>, p1: Int, p2: (Array<(out) Any!>!) -> Game!): Flux<Game!> defined in reactor.core.publisher.Flux
public open fun <O : Any!> zip(p0: (Mutable)Iterable<Publisher<*>!>, p1: Int, p2: Function<in Array<(out) Any!>!, out Game!>): Flux<Game!> defined in reactor.core.publisher.Flux
public open fun <T1 : Any!, T2 : Any!, O : Any!> zip(p0: Publisher<out Game!>, p1: Publisher<out Player!>, p2: BiFunction<in Game!, in Player!, out Tournament!>): Flux<Tournament!> defined in reactor.core.publisher.Flux
public open fun <T1 : Any!, T2 : Any!, T3 : Any!> zip(p0: Publisher<out Game!>, p1: Publisher<out Player!>, p2: Publisher<out Tournament!>): Flux<Tuple3<Game!, Player!, Tournament!>!> defined in reactor.core.publisher.Flux
由于您使用的 zip
函数以 BiFunction
作为参数,因此有效的 Kotlin 等价物如下所示:
val comb = BiFunction<Game, Player, Tournament> { _, _ -> Tournament(...) }
Flux.zip(games, players, comb)
这叫做SAM Conversion。
我有这个 Java 代码:
WebClient webClient = WebClient.create("http://localhost:8080/api/v1");
Flux<Game> games = webClient.get().uri("/games").retrieve().bodyToFlux(Game.class);
Flux<Player> players = webClient.get().uri("/players").retrieve().bodyToFlux(Player.class);
Flux.zip(games, players, (g, p) -> new Tournament(new ArrayList<>(), new ArrayList<>()));
如何将其翻译成 Kotlin?
val webClient = WebClient.create("http://localhost:8080/api/v1")
val games = webClient.get().uri("/games").retrieve().bodyToFlux(Game::class.java)
val players = webClient.get().uri("/players").retrieve().bodyToFlux(Player::class.java)
Flux.zip<Game, Player, Tournament>(games, players) { g, p -> Tournament(emptyList(), emptyList()) }
以上无效,zip 方法不喜欢它的参数:
Error:(24, 14) Kotlin: None of the following functions can be called with the arguments supplied:
@SafeVarargs public final fun <I : Any!, O : Any!> zip(p0: (Array<(out) Any!>!) -> Player!, p1: Int, p2: Array<(out) Publisher<out Game!>!>): Flux<Player!> defined in reactor.core.publisher.Flux
public final fun <T1 : Any!, T2 : Any!, O : Any!> zip(p0: (Subscriber<in Game!>!) -> Unit, p1: (Subscriber<in Player!>!) -> Unit, p2: (Game!, Player!) -> Tournament!): Flux<Tournament!> defined in reactor.core.publisher.Flux
public final fun <T1 : Any!, T2 : Any!, T3 : Any!> zip(p0: (Subscriber<in Game!>!) -> Unit, p1: (Subscriber<in Player!>!) -> Unit, p2: (Subscriber<in Tournament!>!) -> Unit): Flux<Tuple3<Game!, Player!, Tournament!>!> defined in reactor.core.publisher.Flux
public final fun <O : Any!> zip(p0: (Mutable)Iterable<Publisher<*>!>, p1: Int, p2: (Array<(out) Any!>!) -> Game!): Flux<Game!> defined in reactor.core.publisher.Flux
public open fun <O : Any!> zip(p0: (Mutable)Iterable<Publisher<*>!>, p1: Int, p2: Function<in Array<(out) Any!>!, out Game!>): Flux<Game!> defined in reactor.core.publisher.Flux
public open fun <T1 : Any!, T2 : Any!, O : Any!> zip(p0: Publisher<out Game!>, p1: Publisher<out Player!>, p2: BiFunction<in Game!, in Player!, out Tournament!>): Flux<Tournament!> defined in reactor.core.publisher.Flux
public open fun <T1 : Any!, T2 : Any!, T3 : Any!> zip(p0: Publisher<out Game!>, p1: Publisher<out Player!>, p2: Publisher<out Tournament!>): Flux<Tuple3<Game!, Player!, Tournament!>!> defined in reactor.core.publisher.Flux
由于您使用的 zip
函数以 BiFunction
作为参数,因此有效的 Kotlin 等价物如下所示:
val comb = BiFunction<Game, Player, Tournament> { _, _ -> Tournament(...) }
Flux.zip(games, players, comb)
这叫做SAM Conversion。