Package com.linecorp.armeria.client
Class WebClientRequestPreparation
java.lang.Object
com.linecorp.armeria.common.AbstractHttpMessageBuilder
com.linecorp.armeria.common.AbstractHttpRequestBuilder
com.linecorp.armeria.client.WebClientRequestPreparation
- All Implemented Interfaces:
RequestOptionsSetters
,RequestPreparationSetters
,HttpMessageSetters
,HttpRequestSetters
,PathAndQueryParamSetters
,RequestMethodSetters
Prepares and executes a new
HttpRequest
for WebClient
.-
Method Summary
Modifier and TypeMethodDescriptionas
(ResponseAs<HttpResponse, T> responseAs) Sets the specifiedResponseAs
that converts theHttpResponse
into another.asBytes()
Converts the content of theHttpResponse
into bytes.asEntity
(FutureResponseAs<ResponseEntity<T>> responseAs) Writes the content of theHttpResponse
to theFile
.Writes the content of theHttpResponse
to thePath
.asJson
(TypeReference<? extends T> typeRef) Deserializes the JSON content of theHttpResponse
into the specified Java type using the defaultObjectMapper
.asJson
(TypeReference<? extends T> typeRef, ObjectMapper mapper) Deserializes the JSON content of theHttpResponse
into the specified Java type using the specifiedObjectMapper
.Deserializes the JSON content of theHttpResponse
into the specified non-container type using the defaultObjectMapper
.asJson
(Class<? extends T> clazz, ObjectMapper mapper) Deserializes the JSON content of theHttpResponse
into the specified non-container type using the specifiedObjectMapper
.asString()
Converts the content of theHttpResponse
into aString
.attr
(AttributeKey<V> key, V value) Associates the specified value with the givenAttributeKey
in this request.Sets the content for this message.Sets the content for this message.content
(MediaType contentType, CharSequence content) Sets the content for this message.Sets the content for this message.Sets the content for this message.Sets thePublisher
for this message.Sets the content as UTF-8 for this message.Sets the content as UTF-8 for this message.Sets thePublisher
for this message.contentJson
(Object content) Sets the content for this message.Sets a cookie for this request.Sets multiple cookies for this request.Sets DELETE method and path.Disables path parameters substitution.exchangeType
(ExchangeType exchangeType) execute()
Sets GET method and path.Sets HEAD method and path.header
(CharSequence name, Object value) Adds a header for this message.headers
(Iterable<? extends Map.Entry<? extends CharSequence, String>> headers) Adds multiple headers for this message.maxResponseLength
(long maxResponseLength) Sets the maximum allowed length of a server response in bytes.method
(HttpMethod method) Sets the method for this request.Sets OPTIONS method and path.Sets PATCH method and path.Sets the path for this request.Sets a path param for this request.pathParams
(Map<String, ?> pathParams) Sets multiple path params for this request.Sets POST method and path.Sets PUT method and path.queryParam
(String name, Object value) Adds a query param for this request.queryParams
(Iterable<? extends Map.Entry<? extends String, String>> queryParams) Adds multiple query params for this request.requestAutoAbortDelay
(Duration delay) Sets the amount of time to wait before aborting anHttpRequest
when its correspondingHttpResponse
is complete.requestAutoAbortDelayMillis
(long delayMillis) Sets the amount of time in millis to wait before aborting anHttpRequest
when its correspondingHttpResponse
is complete.requestOptions
(RequestOptions requestOptions) Sets the specifiedRequestOptions
that could overwrite the previously configured values such asRequestOptionsSetters.responseTimeout(Duration)
,RequestOptionsSetters.writeTimeout(Duration)
,RequestOptionsSetters.maxResponseLength(long)
andRequestOptionsSetters.attr(AttributeKey, Object)
.responseTimeout
(Duration responseTimeout) responseTimeoutMillis
(long responseTimeoutMillis) Sets TRACE method and path.trailers
(Iterable<? extends Map.Entry<? extends CharSequence, String>> trailers) Adds HTTP trailers for this message.writeTimeout
(Duration writeTimeout) Sets the amount of time allowed until the initial write attempt of the currentRequest
succeeds.writeTimeoutMillis
(long writeTimeoutMillis) Sets the amount of time allowed until the initial write attempt of the currentRequest
succeeds.Methods inherited from class com.linecorp.armeria.common.AbstractHttpRequestBuilder
buildRequest
Methods inherited from class com.linecorp.armeria.common.AbstractHttpMessageBuilder
publisher, trailer
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface com.linecorp.armeria.common.HttpMessageSetters
trailer
-
Method Details
-
execute
-
as
@UnstableApi public <T> TransformingRequestPreparation<HttpResponse,T> as(ResponseAs<HttpResponse, T> responseAs) Sets the specifiedResponseAs
that converts theHttpResponse
into another. -
asEntity
@UnstableApi public <T> FutureTransformingRequestPreparation<ResponseEntity<T>> asEntity(FutureResponseAs<ResponseEntity<T>> responseAs) -
asBytes
Converts the content of theHttpResponse
into bytes. For example:WebClient client = WebClient.of("https://api.example.com"); CompletableFuture<ResponseEntity<byte[]>> response = client.prepare() .get("/v1/items/1") .asBytes() .execute();
-
asString
Converts the content of theHttpResponse
into aString
. For example:WebClient client = WebClient.of("https://api.example.com"); CompletableFuture<ResponseEntity<String>> response = client.prepare() .get("/v1/items/1") .asString() .execute();
-
asFile
Writes the content of theHttpResponse
to thePath
. For example:WebClient client = WebClient.of("https://api.example.com"); CompletableFuture<ResponseEntity<Path>> response = client.prepare() .get("/v1/items/1") .asFile(Paths.get("...")) .execute();
-
asFile
Writes the content of theHttpResponse
to theFile
. For example:WebClient client = WebClient.of("https://api.example.com"); CompletableFuture<ResponseEntity<File>> response = client.prepare() .get("/v1/items/1") .asFile(new File("...")) .execute();
-
asJson
@UnstableApi public <T> FutureTransformingRequestPreparation<ResponseEntity<T>> asJson(Class<? extends T> clazz) Deserializes the JSON content of theHttpResponse
into the specified non-container type using the defaultObjectMapper
. For example:WebClient client = WebClient.of("https://api.example.com"); CompletableFuture<ResponseEntity<MyObject>> response = client.prepare() .get("/v1/items/1") .asJson(MyObject.class) .execute();
Note that this method should NOT be used if the result type is a container such as
Collection
orMap
. UseasJson(TypeReference)
for the container type.- Throws:
InvalidHttpResponseException
- if theHttpStatus
of the response is not success or fails to decode the response body into the result type.- See Also:
-
asJson
@UnstableApi public <T> FutureTransformingRequestPreparation<ResponseEntity<T>> asJson(Class<? extends T> clazz, ObjectMapper mapper) Deserializes the JSON content of theHttpResponse
into the specified non-container type using the specifiedObjectMapper
. For example:ObjectMapper mapper = ...; WebClient client = WebClient.of("https://api.example.com"); CompletableFuture<ResponseEntity<MyObject>> response = client.prepare() .get("/v1/items/1") .asJson(MyObject.class, mapper) .execute();
Note that this method should NOT be used if the result type is a container such as
Collection
orMap
. UseasJson(TypeReference, ObjectMapper)
for the container type.- Throws:
InvalidHttpResponseException
- if theHttpStatus
of the response is not success or fails to decode the response body into the result type.
-
asJson
@UnstableApi public <T> FutureTransformingRequestPreparation<ResponseEntity<T>> asJson(TypeReference<? extends T> typeRef) Deserializes the JSON content of theHttpResponse
into the specified Java type using the defaultObjectMapper
. This method is useful when you want to deserialize the content into a container type such asList
andMap
. For example:WebClient client = WebClient.of("https://api.example.com"); CompletableFuture<ResponseEntity<List<MyObject>>> response = client.prepare() .get("/v1/items/1") .asJson(new TypeReference<List<MyObject>> {}) .execute();
- Throws:
InvalidHttpResponseException
- if theHttpStatus
of the response is not success or fails to decode the response body into the result type.- See Also:
-
asJson
@UnstableApi public <T> FutureTransformingRequestPreparation<ResponseEntity<T>> asJson(TypeReference<? extends T> typeRef, ObjectMapper mapper) Deserializes the JSON content of theHttpResponse
into the specified Java type using the specifiedObjectMapper
. This method is useful when you want to deserialize the content into a container type such asList
andMap
. For example:ObjectMapper mapper = ...; WebClient client = WebClient.of("https://api.example.com"); CompletableFuture<ResponseEntity<List<MyObject>>> response = client.prepare() .get("/v1/items/1") .asJson(new TypeReference<List<MyObject>> {}, mapper) .execute();
- Throws:
InvalidHttpResponseException
- if theHttpStatus
of the response is not success or fails to decode the response body into the result type.
-
requestOptions
Description copied from interface:RequestPreparationSetters
Sets the specifiedRequestOptions
that could overwrite the previously configured values such asRequestOptionsSetters.responseTimeout(Duration)
,RequestOptionsSetters.writeTimeout(Duration)
,RequestOptionsSetters.maxResponseLength(long)
andRequestOptionsSetters.attr(AttributeKey, Object)
.- Specified by:
requestOptions
in interfaceRequestPreparationSetters
-
responseTimeout
Description copied from interface:RequestOptionsSetters
Schedules the response timeout that is triggered when theResponse
is not fully received within the specifiedDuration
since theResponse
started orRequest
was fully sent.Duration.ZERO
disables the limit.- Specified by:
responseTimeout
in interfaceRequestOptionsSetters
-
responseTimeoutMillis
Description copied from interface:RequestOptionsSetters
Schedules the response timeout that is triggered when theResponse
is not fully received within the specifiedresponseTimeoutMillis
since theResponse
started orRequest
was fully sent.0
disables the limit.- Specified by:
responseTimeoutMillis
in interfaceRequestOptionsSetters
-
writeTimeout
Description copied from interface:RequestOptionsSetters
Sets the amount of time allowed until the initial write attempt of the currentRequest
succeeds.Duration.ZERO
disables the limit.- Specified by:
writeTimeout
in interfaceRequestOptionsSetters
-
writeTimeoutMillis
Description copied from interface:RequestOptionsSetters
Sets the amount of time allowed until the initial write attempt of the currentRequest
succeeds.0
disables the limit.- Specified by:
writeTimeoutMillis
in interfaceRequestOptionsSetters
-
maxResponseLength
Description copied from interface:RequestOptionsSetters
Sets the maximum allowed length of a server response in bytes.0
disables the limit.- Specified by:
maxResponseLength
in interfaceRequestOptionsSetters
-
requestAutoAbortDelay
Description copied from interface:RequestOptionsSetters
Sets the amount of time to wait before aborting anHttpRequest
when its correspondingHttpResponse
is complete. This may be useful when you want to send additional data even after the response is complete. SpecifyDuration.ZERO
to abort theHttpRequest
immediately. Any negative value will not abort the request automatically. There is no delay by default.- Specified by:
requestAutoAbortDelay
in interfaceRequestOptionsSetters
-
requestAutoAbortDelayMillis
Description copied from interface:RequestOptionsSetters
Sets the amount of time in millis to wait before aborting anHttpRequest
when its correspondingHttpResponse
is complete. This may be useful when you want to send additional data even after the response is complete. Specify0
to abort theHttpRequest
immediately. Any negative value will not abort the request automatically. There is no delay by default.- Specified by:
requestAutoAbortDelayMillis
in interfaceRequestOptionsSetters
-
attr
Description copied from interface:RequestOptionsSetters
Associates the specified value with the givenAttributeKey
in this request. If this context previously contained a mapping for theAttributeKey
, the old value is replaced by the specified value.- Specified by:
attr
in interfaceRequestOptionsSetters
-
exchangeType
Description copied from interface:RequestOptionsSetters
Sets theExchangeType
that determines whether to stream anHttpRequest
orHttpResponse
. Note that anHttpRequest
will be aggregated before being written ifExchangeType.UNARY
orExchangeType.RESPONSE_STREAMING
is set. If unspecified, theClient
s try to infer a properExchangeType
depending on the content type of a request and a response. Here are examples:WebClient client = WebClient.of("https://armeria.dev"); try (ClientRequestContextCaptor captor = Clients.newContextCaptor()) { client.prepare() .post("/api/v1/items") .contentJson(new Item(...)) // A non-streaming request type. .asString() // A non-streaming response type. .execute(); assert captor.get().exchangeType() == ExchangeType.UNARY; } try (ClientRequestContextCaptor captor = Clients.newContextCaptor()) { client.get("/api/v1/items") // A non-streaming request type. .aggregate(); // A return type is not specified; Assuming that response streaming // is enabled. assert captor.get().exchangeType() == ExchangeType.RESPONSE_STREAMING; } try (ClientRequestContextCaptor captor = Clients.newContextCaptor()) { client.prepare() .post("/api/v1/items") .content(MediaType.JSON_LINES, StreamMessage.of(...)) // A streaming request type. .asFile(Path.get("/path/to/destination")) // A streaming response type. .execute(); assert captor.get().exchangeType() == ExchangeType.BIDI_STREAMING; }
Since a request and a response of
BlockingWebClient
are fully aggregated,ExchangeType.UNARY
is only supported.try (ClientRequestContextCaptor captor = Clients.newContextCaptor()) { AggregatedHttpResponse response = client.blocking().get("/api/v1/items"); assert captor.get().exchangeType() == ExchangeType.UNARY; }
gRPC clients
An
ExchangeType
is automatically inferred from theio.grpc.MethodDescriptor.MethodType
.try (ClientRequestContextCaptor captor = Clients.newContextCaptor()) { Response response = grpcClient.unaryCall(...); assert captor.get().exchangeType() == ExchangeType.UNARY; }
Thrift clients
Thrift protocols do not support streaming.
ExchangeType.UNARY
is only supported.- Specified by:
exchangeType
in interfaceRequestOptionsSetters
-
get
Description copied from interface:RequestMethodSetters
Sets GET method and path.- Specified by:
get
in interfaceRequestMethodSetters
- Overrides:
get
in classAbstractHttpRequestBuilder
-
post
Description copied from interface:RequestMethodSetters
Sets POST method and path.- Specified by:
post
in interfaceRequestMethodSetters
- Overrides:
post
in classAbstractHttpRequestBuilder
-
put
Description copied from interface:RequestMethodSetters
Sets PUT method and path.- Specified by:
put
in interfaceRequestMethodSetters
- Overrides:
put
in classAbstractHttpRequestBuilder
-
delete
Description copied from interface:RequestMethodSetters
Sets DELETE method and path.- Specified by:
delete
in interfaceRequestMethodSetters
- Overrides:
delete
in classAbstractHttpRequestBuilder
-
patch
Description copied from interface:RequestMethodSetters
Sets PATCH method and path.- Specified by:
patch
in interfaceRequestMethodSetters
- Overrides:
patch
in classAbstractHttpRequestBuilder
-
options
Description copied from interface:RequestMethodSetters
Sets OPTIONS method and path.- Specified by:
options
in interfaceRequestMethodSetters
- Overrides:
options
in classAbstractHttpRequestBuilder
-
head
Description copied from interface:RequestMethodSetters
Sets HEAD method and path.- Specified by:
head
in interfaceRequestMethodSetters
- Overrides:
head
in classAbstractHttpRequestBuilder
-
trace
Description copied from interface:RequestMethodSetters
Sets TRACE method and path.- Specified by:
trace
in interfaceRequestMethodSetters
- Overrides:
trace
in classAbstractHttpRequestBuilder
-
method
Description copied from interface:RequestMethodSetters
Sets the method for this request.- Specified by:
method
in interfaceRequestMethodSetters
- Overrides:
method
in classAbstractHttpRequestBuilder
- See Also:
-
path
Description copied from interface:RequestMethodSetters
Sets the path for this request.- Specified by:
path
in interfaceRequestMethodSetters
- Overrides:
path
in classAbstractHttpRequestBuilder
-
content
Description copied from interface:HttpMessageSetters
Sets the content as UTF-8 for this message.- Specified by:
content
in interfaceHttpMessageSetters
- Specified by:
content
in interfaceHttpRequestSetters
- Overrides:
content
in classAbstractHttpRequestBuilder
-
content
Description copied from interface:HttpMessageSetters
Sets the content for this message.- Specified by:
content
in interfaceHttpMessageSetters
- Specified by:
content
in interfaceHttpRequestSetters
- Overrides:
content
in classAbstractHttpRequestBuilder
-
content
Description copied from interface:HttpMessageSetters
Sets the content for this message.- Specified by:
content
in interfaceHttpMessageSetters
- Specified by:
content
in interfaceHttpRequestSetters
- Overrides:
content
in classAbstractHttpRequestBuilder
-
content
@FormatMethod public WebClientRequestPreparation content(@FormatString String format, Object... content) Description copied from interface:HttpMessageSetters
Sets the content as UTF-8 for this message. Thecontent
is formatted byString.format(Locale, String, Object...)
with English locale.- Specified by:
content
in interfaceHttpMessageSetters
- Specified by:
content
in interfaceHttpRequestSetters
- Overrides:
content
in classAbstractHttpRequestBuilder
-
content
@FormatMethod public WebClientRequestPreparation content(MediaType contentType, @FormatString String format, Object... content) Description copied from interface:HttpMessageSetters
Sets the content for this message. Thecontent
is formatted byString.format(Locale, String, Object...)
with English locale.- Specified by:
content
in interfaceHttpMessageSetters
- Specified by:
content
in interfaceHttpRequestSetters
- Overrides:
content
in classAbstractHttpRequestBuilder
-
content
Description copied from interface:HttpMessageSetters
Sets the content for this message. Thecontent
will be wrapped usingHttpData.wrap(byte[])
, so any changes made tocontent
will be reflected in the request.- Specified by:
content
in interfaceHttpMessageSetters
- Specified by:
content
in interfaceHttpRequestSetters
- Overrides:
content
in classAbstractHttpRequestBuilder
-
content
Description copied from interface:HttpMessageSetters
Sets the content for this message.- Specified by:
content
in interfaceHttpMessageSetters
- Specified by:
content
in interfaceHttpRequestSetters
- Overrides:
content
in classAbstractHttpRequestBuilder
-
content
public WebClientRequestPreparation content(org.reactivestreams.Publisher<? extends HttpData> publisher) Description copied from interface:HttpMessageSetters
Sets thePublisher
for this message.- Specified by:
content
in interfaceHttpMessageSetters
- Specified by:
content
in interfaceHttpRequestSetters
- Overrides:
content
in classAbstractHttpRequestBuilder
-
content
public WebClientRequestPreparation content(MediaType contentType, org.reactivestreams.Publisher<? extends HttpData> publisher) Description copied from interface:HttpMessageSetters
Sets thePublisher
for this message.- Specified by:
content
in interfaceHttpMessageSetters
- Specified by:
content
in interfaceHttpRequestSetters
- Overrides:
content
in classAbstractHttpRequestBuilder
-
contentJson
Description copied from interface:HttpMessageSetters
Sets the content for this message. Thecontent
is converted into JSON format using the defaultObjectMapper
.- Specified by:
contentJson
in interfaceHttpMessageSetters
- Specified by:
contentJson
in interfaceHttpRequestSetters
- Overrides:
contentJson
in classAbstractHttpRequestBuilder
-
header
Description copied from interface:HttpMessageSetters
Adds a header for this message.- Specified by:
header
in interfaceHttpMessageSetters
- Specified by:
header
in interfaceHttpRequestSetters
- Overrides:
header
in classAbstractHttpRequestBuilder
-
headers
public WebClientRequestPreparation headers(Iterable<? extends Map.Entry<? extends CharSequence, String>> headers) Description copied from interface:HttpMessageSetters
Adds multiple headers for this message.- Specified by:
headers
in interfaceHttpMessageSetters
- Specified by:
headers
in interfaceHttpRequestSetters
- Overrides:
headers
in classAbstractHttpRequestBuilder
- See Also:
-
trailers
public WebClientRequestPreparation trailers(Iterable<? extends Map.Entry<? extends CharSequence, String>> trailers) Description copied from interface:HttpMessageSetters
Adds HTTP trailers for this message.- Specified by:
trailers
in interfaceHttpMessageSetters
- Specified by:
trailers
in interfaceHttpRequestSetters
- Overrides:
trailers
in classAbstractHttpRequestBuilder
-
pathParam
Description copied from interface:HttpRequestSetters
Sets a path param for this request. For example:HttpRequest.builder() .get("/{foo}") .pathParam("foo", "bar") .build(); // GET `/bar`
- Specified by:
pathParam
in interfaceHttpRequestSetters
- Specified by:
pathParam
in interfacePathAndQueryParamSetters
- Overrides:
pathParam
in classAbstractHttpRequestBuilder
-
pathParams
Description copied from interface:HttpRequestSetters
Sets multiple path params for this request. For example:HttpRequest.builder() .get("/{foo}/:bar") .pathParams(Map.of("foo", 1, "bar", 2)) .build(); // GET `/1/2`
- Specified by:
pathParams
in interfaceHttpRequestSetters
- Specified by:
pathParams
in interfacePathAndQueryParamSetters
- Overrides:
pathParams
in classAbstractHttpRequestBuilder
-
disablePathParams
Description copied from interface:HttpRequestSetters
Disables path parameters substitution. If path parameter is not disabled and a parameter's, specified using{}
or:
, value is not found, anIllegalStateException
is thrown.- Specified by:
disablePathParams
in interfaceHttpRequestSetters
- Specified by:
disablePathParams
in interfacePathAndQueryParamSetters
- Overrides:
disablePathParams
in classAbstractHttpRequestBuilder
-
queryParam
Description copied from interface:HttpRequestSetters
Adds a query param for this request. For example:HttpRequest.builder() .get("/endpoint") .queryParam("foo", "bar") .build(); // GET `/endpoint?foo=bar`
- Specified by:
queryParam
in interfaceHttpRequestSetters
- Specified by:
queryParam
in interfacePathAndQueryParamSetters
- Overrides:
queryParam
in classAbstractHttpRequestBuilder
-
queryParams
public WebClientRequestPreparation queryParams(Iterable<? extends Map.Entry<? extends String, String>> queryParams) Description copied from interface:HttpRequestSetters
Adds multiple query params for this request. For example:HttpRequest.builder() .get("/endpoint") .queryParams(QueryParams.of("from", "foo", "limit", 10)) .build(); // GET `/endpoint?from=foo&limit=10`
- Specified by:
queryParams
in interfaceHttpRequestSetters
- Specified by:
queryParams
in interfacePathAndQueryParamSetters
- Overrides:
queryParams
in classAbstractHttpRequestBuilder
- See Also:
-
cookie
Description copied from interface:HttpRequestSetters
Sets a cookie for this request. For example:HttpRequest.builder() .get("/") .cookie(Cookie.ofSecure("cookie", "foo")) .build();
- Specified by:
cookie
in interfaceHttpMessageSetters
- Specified by:
cookie
in interfaceHttpRequestSetters
- Overrides:
cookie
in classAbstractHttpRequestBuilder
- See Also:
-
cookies
Description copied from interface:HttpRequestSetters
Sets multiple cookies for this request. For example:HttpRequest.builder() .get("/") .cookies(Cookies.ofSecure(Cookie.ofSecure("cookie1", "foo"), Cookie.ofSecure("cookie2", "bar"))) .build();
- Specified by:
cookies
in interfaceHttpMessageSetters
- Specified by:
cookies
in interfaceHttpRequestSetters
- Overrides:
cookies
in classAbstractHttpRequestBuilder
- See Also:
-