reactive programming – Spring webflux interact with Mono object

reactive programming – Spring webflux interact with Mono object

I always find the answer just after asking the question…
I just used another flatmap to edit the object:

@PutMapping({id})
public Mono<Server> update(@PathVariable UUID id, @RequestBody Server updatedServer) {
    Mono<Server> server = this.serverRepository.findById(id);

    return server.flatMap(value -> {
        value.setName(updatedServer.getName());
        value.setHost(updatedServer.getHost());
        return Mono.just(value); // Can this be done cleaner?
    }).flatMap(this.serverRepository::save);
}

Or when still returning success: true:

@PutMapping({id})
public Mono<Map> update(@PathVariable UUID id, @RequestBody Server updatedServer) {
    Mono<Server> server = this.serverRepository.findById(id);

    return server.flatMap(value -> {
        value.setName(updatedServer.getName());
        value.setHost(updatedServer.getHost());
        return Mono.just(value); // Can this be done cleaner?
    }).flatMap(this.serverRepository::save).subscribe();

    return Mono.just(Collections.singletonMap(success, true));
}

reactive programming – Spring webflux interact with Mono object

Leave a Reply

Your email address will not be published. Required fields are marked *