-1

I have been strugling against this for two days now, and finally reach a dead end.

Thing is, I'm handling a request like this:

public Mono<ServerResponse> jsonBodyDemo(ServerRequest request) {  

    request.bodyToMono(DemoDTO.class).log().subscribe(
        d -> System.out.println(d.getName()),
        e -> System.out.println("error"));

    return ServerResponse.ok().body(BodyInserters.fromValue("OK"));
}

And the name is never logged. All I get is:

2020-04-29 14:46:39.210  INFO 20404 --- [ctor-http-nio-3] reactor.Mono.OnErrorResume.1             : onSubscribe(FluxOnErrorResume.ResumeSubscriber)
2020-04-29 14:46:39.210  INFO 20404 --- [ctor-http-nio-3] reactor.Mono.OnErrorResume.1             : request(unbounded)

But the actual onNext never happens

Any suggestion about what I'm missing?

  • you can use Thomas Andolf's answer. Just a little bit of change. Rather returning `ServerResponse.ok().body(BodyInserters.fromValue("OK"))` from `flatmap` you return it by chaining `then(ServerResponse.ok().body(BodyInserters.fromValue("OK")))` after `flatmap`. and in the `flatmap` start your background process. and don't forget to return the mono chain from the `flatMap` – Shoshi Apr 30 '20 at 15:58

1 Answers1

0

You should almost never subscribe in a webflux application. Your service is a producer, and the one initiating the call is the subscriber (the calling client)

public Mono<ServerResponse> jsonBodyDemo(ServerRequest request) {  
    return request.bodyToMono(DemoDTO.class)
             .flatMap(dto -> {
                 System.out.println(dto.getName());
                 return ServerResponse.ok().body(BodyInserters.fromValue("OK"));
             });
}

When you subscribe, you break the chain, think of everything as a flow where everything needs to be connected.

Toerktumlare
  • 12,548
  • 3
  • 35
  • 54
  • The code I posted is a mere sample. Actually, what I need to do, is to get the body, launch a background process that uses the content of the body, what I'm failing to get, and then return some mono. – pablo lamberti Apr 30 '20 at 14:17
  • Downvoted the question, because the question starter expects us to be mind readers and know what his `actual` code looks like. – Toerktumlare Apr 30 '20 at 15:47