Troy B
Troy B

Reputation: 31

Mono<Object> being returned instead of Mono<ResponseEntity> when mapping (Java 8)

Trying to practice reactive coding for an API but I'm struggling to understand what I'm doing wrong when using flatMap() and map() to cast to a ResponseEntity object. The error mentions that the code is returning a Mono<Object> and cant be cast/transformed into a Mono<ResponseEntity<>>.

Public Mono<ResponseEntity<?> deleteEndpoint(String someId) {
  return db.existsById(someId).flatMap(exists -> {
    if (!exists) return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
    else {
      Mono<Boolean> deletedStuff1 = someFunct();
      Mono<Boolean> deletedStuff2 = anotherFunct();
     
      Mono<Tuple2<Boolean, Boolean>> deletedStuff = Mono.zip(deletedStuff1, deletedStuff2);

      return deletedStuff.then(Mono.just(ResponseEntity.status(NO_CONTENT).build());
    }
  });
}

All help is appreciated

Upvotes: 2

Views: 820

Answers (1)

kerbermeister
kerbermeister

Reputation: 4201

From .flatMap() you must return Publisher, not actual object

In this if statement you return ResponseEntity instead of Mono<ResponseEntity>

So, just wrap it with Mono

if (!exists) {
    return Mono.just(ResponseEntity.status(HttpStatus.BAD_REQUEST).build());
} else {
    // ...

Upvotes: 2

Related Questions