0

I have this GET controller

@GetMapping("/test")
public Flux<String> test() {
  return Flux.just("Test1", "test2")
    .delayElements(Duration.ofSeconds(1))
    .log();
}

When I call this, it takes 2 seconds to complete, the log prints the onNext and onComplete which mean it is emitting but it is returning empty.

Any idea?

If I change this and use block() method and remove the Flux, it is returing "Test1" and "Test2".

1 Answer 1

0

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")
}
3
  • I tried the same you show, nothing. The spring-boot version I have is 2.6.4.
    – Maverlck
    Commented Jun 9 at 2:27
  • Hi @Maverlck. Updated my answer with a downgrade to 2.6.4. Code is still working on my computer. BR
    – Roar S.
    Commented Jun 9 at 7:36
  • 1
    I created a new project, you are right. This code snippet works as expected. Let me see why this old project is not working. I'll keep everybody posted
    – Maverlck
    Commented Jun 9 at 17:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.