2

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?

Fran b
  • 3,016
  • 6
  • 38
  • 65
  • Fire an application event? Does it need to be atomic - in which case perhaps decoupling doesn’t make sense? – Boris the Spider Nov 14 '20 at 12:03
  • Mmh... I think you are right, this should be atomic because the object persistence just must happen when is saved on the database and its visibility is set on this third-party system. But I would like a global solution to avoid repeating the call to third-party on every controller. – Fran b Nov 14 '20 at 12:17
  • Add a service that does both and call that service instead? – Boris the Spider Nov 14 '20 at 12:46

0 Answers0