Update 2024-06-09: Downgraded Spring Boot to 2.6.4 after comment from OP. Code in this answer is still working with 2.6.4.
With a controller like this, your code works on my computer. Endpoint http://localhost:8080/test2 is fetching content from http://localhost:8080/test1 using WebClient
.
package com.example.flux_demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;
import java.time.Duration;
@RestController
public class MyController {
@GetMapping("/test")
public Flux<String> test() {
return Flux.just("Test1", "test2")
.delayElements(Duration.ofSeconds(1))
.log();
}
@GetMapping("/test2")
public Flux<String> test2() {
return WebClient.create("http://localhost:8080")
.get()
.uri("/test")
.retrieve()
.bodyToFlux(String.class);
}
}
build.gradle.kts
plugins {
java
id("org.springframework.boot") version "2.6.4"
id("io.spring.dependency-management") version "1.1.5"
}
group = "com.example"
version = "0.0.1-SNAPSHOT"
java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}
repositories {
mavenCentral()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-webflux")
}