salman irfan khandu
salman irfan khandu

Reputation: 149

Spring Reactive API response blank body while transforming Mono<Void> to Mono<Response>

I have a service call that returns Mono. Now while giving API response to user I want to send some response. I tried with flatMap as well as a map but it won't work. It gives me an empty body in response.

Here the code sample

    //Service Call
    public Mono<Void> updateUser(....) {
        .......... return Mono<Void>...
    }

   //Rest Controller
   @PostMapping(value = "/user/{id})
    public Mono<Response> update(......) {
        return service.updateUser(....)
                .map(r -> new Response(200, "User updated successfully"));

    }

When I hit the above API it gives me an empty body in response. Can anyone please help me to get body in API response?

Thank you

Upvotes: 1

Views: 1674

Answers (1)

Toerktumlare
Toerktumlare

Reputation: 14712

if you wish to ignore the return value from a Mono or Flux and just trigger something next in the chain you can use either the then operator, or the thenReturn operator.

The difference is that then takes a Mono while thenReturn takes a concrete value (witch will get wrapped in a Mono.

@PostMapping(value = "/user/{id})
public Mono<Response> update(......) {
    return service.updateUser(....)
            .thenReturn(new Response(200, "User updated successfully"));
}

Upvotes: 1

Related Questions