Spring Webflux: There is any way to modify a controller response?

Viewed 194

When a POST request is made to an endpoint controller, this one creates an object on the database and responds with a Mono<ResponseEntity<Entity>>. But before sending back the response to the client, the objects successfully created on the database have to be registered on another system, calling its API.

class FooController(
   val repo: ReactiveCrudRepository<Foo,String>
) {
  @PostMapping
  fun create(@RequestBody dto: DTO) = repo.save(dto.toEntity() as E)
      .map {
        println("Hey, I created the obj in the db")
        val location = URI(this.javaClass.getAnnotation(RequestMapping::class.java).value[0] + "/" + it.id)  created(location).body(it)
        // Just before responding, at this moment, the object must be sent to a third-party API.
      }
}

How can I decouple the call from the controller using a Webfilter, Handler, Listener, ... to perform this call?

0 Answers
Related