A
BodySubscriber
consumes response body bytes and converts them into a higher-level Java type. The class
BodySubscribers
provides implementations of many common body subscribers.
The object acts as a Flow.Subscriber
<List
<ByteBuffer
>> to the HTTP Client implementation, which publishes lists of ByteBuffers containing the response body. The Flow of data, as well as the order of ByteBuffers in the Flow lists, is a strictly ordered representation of the response body. Both the Lists and the ByteBuffers, once passed to the subscriber, are no longer used by the HTTP Client. The subscriber converts the incoming buffers of data to some higher-level Java type T
.
The getBody()
method returns a CompletionStage
<T>
that provides the response body object. The CompletionStage
must be obtainable at any time. When it completes depends on the nature of type T
. In many cases, when T
represents the entire body after being consumed then the CompletionStage
completes after the body has been consumed. If T
is a streaming type, such as InputStream
, then it completes before the body has been read, because the calling code uses the InputStream
to consume the data.