Class RestClient
- All Implemented Interfaces:
Closeable,AutoCloseable
Create instances via create() or builder():
By default, RestClient uses JavaHttpTransport (backed by the JDK's
HttpClient), which ships built into the juneau-rest-client artifact and
requires no extra dependencies on Java 11+. Pulling in one of the optional transport modules
(juneau-rest-client-apache-httpclient-45, -apache-httpclient-50, -okhttp,
-jetty) registers a higher-priority provider via ServiceLoader that takes
over automatically. An explicit RestClient.Builder.transport(HttpTransport) call always wins.
See Also:
- Since:
- 9.2.1
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final List<BodyConverter<?>>Default body converters applied in order whenRestRequest.body(Object)is called. -
Method Summary
Modifier and TypeMethodDescriptionstatic RestClient.Builderbuilder()Returns a newRestClient.Builder.voidclose()static RestClientcreate()Creates a newRestClientusing auto-discovered transport.Creates a DELETE request to the given URL.Creates a GET request to the given URL.Returns the per-client special-purpose address ranges this client re-permits in the SSRF guardrail (seeRestClient.Builder.allowAddressRanges(String...)).Returns the defaultAcceptheader value advertising what this client can read.Returns the explicitly-configured default serializer used for outbound bodies when the format is not otherwise discernable.getMatchingParser(String contentType) Returns the parser matching the given responseContent-Type, or the explicitly-configured default parser.getParserForMediaType(String mediaType) Returns the registered parser matching the given media type, orOptional.empty()if none matches.getSerializerForMediaType(String mediaType) Returns the registered request serializer matching the given media type, orOptional.empty()if none matches.Returns the underlying transport used by this client.Creates a HEAD request to the given URL.booleanReturns the effective builder/settings-levelallowPrivateUrlsopt-in for this client (the logical OR ofRestClient.Builder.allowPrivateUrls(boolean)and the "RestClient.allowPrivateUrls" system property).Creates a PATCH request to the given URL.Creates a POST request to the given URL.Creates a PUT request to the given URL.<T> TCreates a Java proxy for the givenRemote-annotated interface.Creates a request with the given HTTP method and URL.
-
Field Details
-
DEFAULT_BODY_CONVERTERS
Default body converters applied in order whenRestRequest.body(Object)is called.Custom converters registered via
RestClient.Builder.bodyConverter(BodyConverter[])are prepended to this list. CallRestClient.Builder.bodyConverters(BodyConverter[])to replace all defaults.
-
-
Method Details
-
isAllowPrivateUrls
Returns the effective builder/settings-levelallowPrivateUrlsopt-in for this client (the logical OR ofRestClient.Builder.allowPrivateUrls(boolean)and the "RestClient.allowPrivateUrls" system property).The fully effective per-call value additionally ORs in the
@Remote/@RemoteOpallowPrivateUrlsannotation attribute (seeRemote.allowPrivateUrls()).- Returns:
trueif this client opts into allowing private/loopback/link-local/metadata@Remotetargets.
-
getAllowedAddressRanges
Returns the per-client special-purpose address ranges this client re-permits in the SSRF guardrail (seeRestClient.Builder.allowAddressRanges(String...)). They are unioned with the "RestClient.allowAddressRanges" system property at each check.- Returns:
- The allowed ranges. Never
null .
-
create
Creates a newRestClientusing auto-discovered transport.- Returns:
- A new instance. Never
null .
-
builder
Returns a newRestClient.Builder. -
get
Creates a GET request to the given URL.- Parameters:
url- The target URL. Must not benull .- Returns:
- A new
RestRequest. Nevernull .
-
post
Creates a POST request to the given URL.- Parameters:
url- The target URL. Must not benull .- Returns:
- A new
RestRequest. Nevernull .
-
put
Creates a PUT request to the given URL.- Parameters:
url- The target URL. Must not benull .- Returns:
- A new
RestRequest. Nevernull .
-
patch
Creates a PATCH request to the given URL.- Parameters:
url- The target URL. Must not benull .- Returns:
- A new
RestRequest. Nevernull .
-
delete
Creates a DELETE request to the given URL.- Parameters:
url- The target URL. Must not benull .- Returns:
- A new
RestRequest. Nevernull .
-
head
Creates a HEAD request to the given URL.- Parameters:
url- The target URL. Must not benull .- Returns:
- A new
RestRequest. Nevernull .
-
request
Creates a request with the given HTTP method and URL.- Parameters:
method- The HTTP method. Must not benull .url- The target URL. Must not benull .- Returns:
- A new
RestRequest. Nevernull .
-
getTransport
Returns the underlying transport used by this client.- Returns:
- The transport. Never
null .
-
getDefaultSerializer
Returns the explicitly-configured default serializer used for outbound bodies when the format is not otherwise discernable.Only an explicit default set via
RestClient.Builder.defaultSerializer(Serializer)is honored — there is no implicit JSON fallback and no lone-registered-entry fallback. A fully-unconfigured client returnsOptional.empty().- Returns:
- The configured default serializer, or
Optional.empty()if none was set.
-
getMatchingParser
Returns the parser matching the given responseContent-Type, or the explicitly-configured default parser.Resolution precedence: an exact media-type match against the client's
ParserSetwins; otherwise the explicitly-configureddefault parseris used; otherwiseOptional.empty()is returned. There is no implicit JSON fallback and no lone-registered-entry fallback, so a fully-unconfigured client resolves to empty (callers throw415 Unsupported Media Type ).- Parameters:
contentType- The responseContent-Typeheader value. May benull .- Returns:
- The matching parser, or
Optional.empty()if nothing matched and no default parser is configured.
-
getSerializerForMediaType
Returns the registered request serializer matching the given media type, orOptional.empty()if none matches.Unlike
getDefaultSerializer(), this method performs media-type-driven selection against the client'sSerializerSet(viaSerializerSet.getSerializer(String)) and returnsOptional.empty()on no match so the caller can apply the locked no-match fallback (use the default serializer but still send the overriddenContent-Typelabel). An explicitly-configured single default serializer is also consulted when no set is registered.- Parameters:
mediaType- The desired request media type. May benull /empty.- Returns:
- The matching serializer, or
Optional.empty()if no registered serializer matches.
-
getParserForMediaType
Returns the registered parser matching the given media type, orOptional.empty()if none matches.Used by the next-gen
RemoteClientas theacceptfallback parser: it is consulted only when the responseContent-Typematched no registered parser (or the response was unlabeled). ReturnsOptional.empty()on no match so the caller can fall back to the default parser.- Parameters:
mediaType- The desired parse media type. May benull /empty.- Returns:
- The matching parser, or
Optional.empty()if no registered parser matches.
-
getDefaultAccept
Returns the defaultAcceptheader value advertising what this client can read.- Returns:
- The default
Acceptheader value, ornull if no parsers and no default parser are configured (no implicitapplication/jsonfallback).
-
remote
Creates a Java proxy for the givenRemote-annotated interface.Each method call on the returned proxy will be translated into an HTTP request using this client.
UserService
svc = client.remote(UserService.class ); Stringuser =svc .getUser("42" );- Type Parameters:
T- The interface type.- Parameters:
iface- The interface class. Must be annotated withRemote. Must not benull .- Returns:
- A proxy instance backed by this client. Never
null . - Throws:
IllegalArgumentException- Ififaceis not an interface or not annotated with@Remote.
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-