Interface Exchange<A extends ExchangeContext>

Type Parameters:
A - the type of the exchange context
All Superinterfaces:
BaseExchange<A,Request,Mono<? extends Response>>

public interface Exchange<A extends ExchangeContext> extends BaseExchange<A,Request,Mono<? extends Response>>

Represents an HTTP client exchange (request/response) between a client and a server.

An HTTP client exchange is obtained from an Endpoint and allows to build and send an HTTP request or to open a WebSocket to an HTTP server repesented by that endpoint.

The HTTP request is only sent when the exchange's response publisher is subscribed and if it hasn't been intercepted by an ExchangeInterceptor which is then responsible for providing the response.


 endpoint.exchange(Method.POST, "/")
 	.flatMap(exchange -> {
		exchange.request()
			.headers(headers -> headers.contentType("text/plain"))
			.body().get().string().value("This is a request body");
		return exchange.response();
   })
  .subscribe(response -> {}); // send the request
 

As for an HTTP request, a WebSocket is only created when the WebSocket exchange publisher is subscribed.


 endpoint.exchange("/")
 	.flatMap(Exchange::webSocket)
  .subscribe(wsExchange -> {}); // create a WebSocket connection
 
Since:
1.6
Author:
Jeremy Kuhn
See Also:
  • Method Details

    • webSocket

      default Mono<? extends WebSocketExchange<A>> webSocket()

      Returns a WebSocket exchange publisher.

      The WebSocket connection and the corresponding WebSocketExchange are created when the returned publisher is subscribed.

      Returns:
      a WebSocket exchange mono
    • webSocket

      Mono<? extends WebSocketExchange<A>> webSocket(String subProtocol)

      Returns a WebSocket exchange publisher requesting the specified subprotocol.

      The WebSocket connection and the corresponding WebSocketExchange are created when the returned publisher is subscribed.

      Parameters:
      subProtocol - the subprotocol requested to the server by the client.
      Returns:
      a WebSocket exchange mono