Sobes.tech
Junior — Middle

Have you had experience organizing asynchronous data exchange using REST API?

sobes.tech AI

Answer from AI

Yes, when using REST API for asynchronous data exchange, approaches that do not block the client during request processing are often employed. For example, you can implement the "Polling" pattern — the client periodically polls the server for the result readiness, or use Webhooks, where the server notifies the client about an event.

In Java with Spring, you can use @Async for asynchronous method execution, as well as return CompletableFuture or DeferredResult from controllers to handle requests asynchronously.

An example using DeferredResult:

@GetMapping("/process")
public DeferredResult<String> process() {
    DeferredResult<String> output = new DeferredResult<>();
    new Thread(() -> {
        // Long operation
        try { Thread.sleep(3000); } catch (InterruptedException e) {}
        output.setResult("Processing result");
    }).start();
    return output;
}

This approach allows not to block the server thread and to return a response to the client when the operation completes.

Have you had experience organizing asynchronous data… - sobes.tech