1

I have a normal spring boot application with controller and service like following:

@RestController
class EventController(private val service: Service) {

  @GetMapping(value = ["xxx"], provides = [APPLICATION_JSON_VALUE])
  @ResponseStatus(HttpStatus.OK)
  fun get() {
    // call service?? is that right way?
    service.get()
  }
}

class Service {
  fun get(): Future<String> {
    xxx
  }
}

However, because some of third party API I used, let's say Kafka Producer API, and the service has to return a java future (java.util.concurrent.Future) and how should I handle this future result in my controller?

Anton Malyshev
  • 8,686
  • 2
  • 27
  • 45
ttt
  • 3,934
  • 8
  • 46
  • 85
  • What do you want to do with the result of the `Future`? If you just need to pass the result of the `Future` then simply return the `Future`. – M. Deinum Sep 12 '18 at 09:27
  • why not use **service.get().get()** to get the result – clevertension Sep 12 '18 at 09:28
  • @clevertension yes, that's what I am currently doing but after read some articles and all recommend not calling `get()` method on a future in production code. – ttt Sep 12 '18 at 09:54
  • A spring controller can be async in nature, so just return it and spring will do the handling of the result. – M. Deinum Sep 12 '18 at 10:01

0 Answers0