Łukasz Urbański
Łukasz Urbański

Reputation: 51

How to send data piece by piece using Flux?

I play with baeldung tutorial from github. I wanted to see how a browser retrieves data piece by piece. So I added my simple controller method:

    @GetMapping("/flux")
    public Flux<Employee> getFlux() {
        return Flux.fromIterable(employeeRepository.employeeData.values())
                .delayElements(Duration.ofMillis(2_000))
                .take(3);
    }

But when I look at the browser network the data is retrieved in one chunk after 6 second delay. How to do this correctly?

Upvotes: 0

Views: 552

Answers (1)

Vimalesh Jeyavelmani
Vimalesh Jeyavelmani

Reputation: 19

This is happening because you didn't mention Media type for the response.

Try this code.

@GetMapping(value = "/test",produces = MediaType.APPLICATION_STREAM_JSON_VALUE)
public Flux getMapping() {
    return Flux.interval(Duration.ofMillis(300)).map(f -> "HI");
}

Thanks,
Vimalesh

Upvotes: 1

Related Questions