Nicolas Ocquidant
Nicolas Ocquidant

Reputation: 127

Get response from TCP server in RestController

My Spring webflux controller needs to access a remote TCP server. How could I stream the response from the TCP server to the client?

Something like:

WebClient client = WebClient.create("http://example.com");

....

@GetMapping(value = "/account")
public Mono<String> account() {
  Mono<Account> account = client.get()
        .url("/accounts/{id}", 1L)
        .accept(APPLICATION_JSON)
        .exchange(request)
        .then(response -> response.bodyToMono(Account.class));

  return account;
}

But with a TcpClient (not WebClient).

Thanks,

--nick

Upvotes: 0

Views: 1231

Answers (1)

Simon Basl&#233;
Simon Basl&#233;

Reputation: 28301

Unfortunately, Spring WebFlux is indeed a bit focused on HTTP.

There is a TcpClient in reactor-netty, but that's more low level and necessitate a bit of netty knowledge (as you will have to perform stuff like framing and decoding yourself). And it doesn't have the notion of a Response like the HttpClient does, so it's not easily bridged into a Mono/Flux that you can then transform in your controller...

So I'm afraid at this point there is no off-the-shelf solution to that particular use case :/ I suggest you open an issue though.

Upvotes: 3

Related Questions