public class RestClient extends BeanContext implements Closeable
Serializer
class.
Parser
class.
Modifier and Type | Field and Description |
---|---|
static String |
RESTCLIENT_debug
Configuration property: Debug.
|
static String |
RESTCLIENT_executorService
Configuration property: Executor service.
|
static String |
RESTCLIENT_executorServiceShutdownOnClose
Configuration property: Shut down executor service on close.
|
static String |
RESTCLIENT_headers
Configuration property: Request headers.
|
static String |
RESTCLIENT_interceptors
Configuration property: Call interceptors.
|
static String |
RESTCLIENT_interceptors_add
Add to the Call interceptors property.
|
static String |
RESTCLIENT_keepHttpClientOpen
Configuration property: Keep HttpClient open.
|
static String |
RESTCLIENT_parser
Configuration property: Parser.
|
static String |
RESTCLIENT_partSerializer
Configuration property: Part serializer.
|
static String |
RESTCLIENT_query
Configuration property: Request query parameters.
|
static String |
RESTCLIENT_retries
Configuration property: Number of retries to attempt.
|
static String |
RESTCLIENT_retryInterval
Configuration property: The time in milliseconds between retry attempts.
|
static String |
RESTCLIENT_retryOn
Configuration property: Retry-on determination object.
|
static String |
RESTCLIENT_rootUri
Configuration property: Root URI.
|
static String |
RESTCLIENT_serializer
Configuration property: Serializer.
|
BEAN_beanClassVisibility, BEAN_beanConstructorVisibility, BEAN_beanDictionary, BEAN_beanDictionary_add, BEAN_beanDictionary_remove, BEAN_beanFieldVisibility, BEAN_beanFilters, BEAN_beanFilters_add, BEAN_beanFilters_remove, BEAN_beanMapPutReturnsOldValue, BEAN_beanMethodVisibility, BEAN_beansRequireDefaultConstructor, BEAN_beansRequireSerializable, BEAN_beansRequireSettersForGetters, BEAN_beansRequireSomeProperties, BEAN_beanTypePropertyName, BEAN_debug, BEAN_excludeProperties, BEAN_ignoreInvocationExceptionsOnGetters, BEAN_ignoreInvocationExceptionsOnSetters, BEAN_ignorePropertiesWithoutSetters, BEAN_ignoreUnknownBeanProperties, BEAN_ignoreUnknownNullBeanProperties, BEAN_implClasses, BEAN_includeProperties, BEAN_locale, BEAN_mediaType, BEAN_notBeanClasses, BEAN_notBeanClasses_add, BEAN_notBeanClasses_remove, BEAN_notBeanPackages, BEAN_notBeanPackages_add, BEAN_notBeanPackages_remove, BEAN_pojoSwaps, BEAN_pojoSwaps_add, BEAN_pojoSwaps_remove, BEAN_propertyNamer, BEAN_sortProperties, BEAN_timeZone, BEAN_useInterfaceProxies, BEAN_useJavaBeanIntrospector, DEFAULT, DEFAULT_SORTED
Modifier and Type | Method and Description |
---|---|
RestClientBuilder |
builder()
Creates a builder from this context object.
|
void |
close()
Calls
Closeable.close() on the underlying CloseableHttpClient . |
void |
closeQuietly()
Same as
close() , but ignores any exceptions. |
static RestClientBuilder |
create()
Instantiates a new clean-slate
RestClientBuilder object. |
static RestClientBuilder |
create(Class<? extends Serializer> s,
Class<? extends Parser> p)
Instantiates a new
RestClientBuilder object using the specified serializer and parser. |
static RestClientBuilder |
create(Serializer s,
Parser p)
Instantiates a new
RestClientBuilder object using the specified serializer and parser. |
RestCall |
doCall(HttpMethod method,
Object url,
Object content)
Perform a generic REST call.
|
RestCall |
doCall(String method,
Object url,
boolean hasContent)
Perform a generic REST call.
|
RestCall |
doCallback(String callString)
Performs a REST call where the entire call is specified in a simple string.
|
RestCall |
doDelete(Object url)
Perform a
DELETE request against the specified URL. |
RestCall |
doFormPost(Object url,
Object o)
Perform a
POST request with a content type of application/x-www-form-urlencoded
against the specified URL. |
RestCall |
doGet(Object url)
Perform a
GET request against the specified URL. |
RestCall |
doOptions(Object url)
Perform an
OPTIONS request against the specified URL. |
RestCall |
doPost(Object url)
Same as
doPost(Object, Object) but don't specify the input yet. |
RestCall |
doPost(Object url,
Object o)
Perform a
POST request against the specified URL. |
RestCall |
doPut(Object url)
Same as
doPut(Object, Object) but don't specify the input yet. |
RestCall |
doPut(Object url,
Object o)
Perform a
PUT request against the specified URL. |
protected org.apache.http.HttpResponse |
execute(org.apache.http.client.methods.HttpUriRequest req)
Execute the specified request.
|
protected void |
finalize() |
<T> T |
getRemoteableProxy(Class<T> interfaceClass)
Create a new proxy interface against a REST interface.
|
<T> T |
getRemoteableProxy(Class<T> interfaceClass,
Object restUrl)
Same as
getRemoteableProxy(Class) except explicitly specifies the URL of the REST interface. |
<T> T |
getRemoteableProxy(Class<T> interfaceClass,
Object restUrl,
Serializer serializer,
Parser parser)
Same as
getRemoteableProxy(Class, Object) but allows you to override the serializer and parser used. |
_class, asMap, createBeanSession, createBeanSession, createDefaultBeanSessionArgs, createDefaultSessionArgs, createSession, createSession, createSession, dumpCacheStats, getBeanMeta, getBeanRegistry, getBeanTypePropertyName, getClassMeta, getClassMeta, getClassMetaForObject, getExcludeProperties, getImplClassConstructor, getIncludeProperties, hasSameCache, isBean, isNotABean, newInstance, newInstance, newInstanceFromOuter, object, resolveClassMeta, string
equals, getArrayProperty, getArrayProperty, getBooleanProperty, getClassArrayProperty, getClassArrayProperty, getClassArrayProperty, getClassListProperty, getClassListProperty, getClassMapProperty, getClassMapProperty, getClassProperty, getClassSetProperty, getClassSetProperty, getInstanceArrayProperty, getInstanceArrayProperty, getInstanceArrayProperty, getInstanceProperty, getInstanceProperty, getInstanceProperty, getIntegerProperty, getListProperty, getListProperty, getLongProperty, getMapProperty, getProperty, getProperty, getPropertyKeys, getPropertyStore, getSetProperty, getSetProperty, getStringProperty, hashCode, toString
public static final String RESTCLIENT_debug
Boolean
Enable debug mode.
public static final String RESTCLIENT_executorService
Class<? implements ExecutorService>
or ExecutorService
.
Defines the executor service to use when calling future methods on the RestCall
class.
This executor service is used to create Future
objects on the following methods:
RestCall.runFuture()
RestCall.getResponseFuture(Class)
RestCall.getResponseFuture(Type,Type...)
RestCall.getResponseAsString()
The default executor service is a single-threaded ThreadPoolExecutor
with a 30 second timeout
and a queue size of 10.
public static final String RESTCLIENT_executorServiceShutdownOnClose
Boolean
Call ExecutorService.shutdown()
when close()
is called.
public static final String RESTCLIENT_headers
Map<String,String>
Headers to add to every request.
public static final String RESTCLIENT_interceptors
List<Class<? implements RestCallInterceptor> | RestCallInterceptor>
Interceptors that get called immediately after a connection is made.
public static final String RESTCLIENT_interceptors_add
public static final String RESTCLIENT_keepHttpClientOpen
Boolean
Don't close this client when the close()
method is called.
public static final String RESTCLIENT_parser
Class<? extends Parser>
or Parser
.
JsonParser
;
The parser to use for parsing POJOs in response bodies.
public static final String RESTCLIENT_partSerializer
Class<? implements HttpPartSerializer>
or HttpPartSerializer
.
SimpleUonPartSerializer
;
The serializer to use for serializing POJOs in form data, query parameters, headers, and path variables.
public static final String RESTCLIENT_query
Map<String,String>
Query parameters to add to every request.
public static final String RESTCLIENT_retries
Integer
1
The number of retries to attempt when the connection cannot be made or a >400
response is received.
public static final String RESTCLIENT_retryInterval
Integer
-1
The time in milliseconds between retry attempts.
-1
means retry immediately.
public static final String RESTCLIENT_retryOn
Class<? extends RetryOn
or RetryOn
RetryOn.DEFAULT
Object used for determining whether a retry should be attempted.
public static final String RESTCLIENT_rootUri
String
When set, relative URL strings passed in through the various rest call methods (e.g. doGet(Object)
will be prefixed with the specified root.
This root URL is ignored on those methods if you pass in a URL
, URI
, or an absolute URL string.
Trailing slashes are trimmed.
public static final String RESTCLIENT_serializer
Class<? extends Serializer>
or Serializer
.
JsonSerializer
;
The serializer to use for serializing POJOs in request bodies.
public static RestClientBuilder create()
RestClientBuilder
object.RestClientBuilder
object.public static RestClientBuilder create(Serializer s, Parser p)
RestClientBuilder
object using the specified serializer and parser.
Shortcut for calling RestClient.
s
- The serializer to use for output.p
- The parser to use for input.RestClientBuilder
object.public static RestClientBuilder create(Class<? extends Serializer> s, Class<? extends Parser> p)
RestClientBuilder
object using the specified serializer and parser.
Shortcut for calling RestClient.
s
- The serializer class to use for output.p
- The parser class to use for input.RestClientBuilder
object.public RestClientBuilder builder()
Context
Builders are used to define new contexts (e.g. serializers, parsers) based on existing configurations.
builder
in class BeanContext
public void close() throws IOException
Closeable.close()
on the underlying CloseableHttpClient
.
It's good practice to call this method after the client is no longer used.
close
in interface Closeable
close
in interface AutoCloseable
IOException
public void closeQuietly()
close()
, but ignores any exceptions.protected org.apache.http.HttpResponse execute(org.apache.http.client.methods.HttpUriRequest req) throws Exception
Subclasses can override this method to provide specialized handling.
req
- The HTTP request.Exception
public RestCall doGet(Object url) throws RestCallException
GET
request against the specified URL.url
- The URL of the remote REST resource.
Can be any of the following: String
, URI
, URL
.RestCall
object that can be further tailored before executing the request and getting the response
as a parsed object.RestCallException
- If any authentication errors occurred.public RestCall doPut(Object url, Object o) throws RestCallException
PUT
request against the specified URL.url
- The URL of the remote REST resource.
Can be any of the following: String
, URI
, URL
.o
- The object to serialize and transmit to the URL as the body of the request.
Can be of the following types:
Reader
- Raw contents of Reader
will be serialized to remote resource.
InputStream
- Raw contents of InputStream
will be serialized to remote resource.
Object
- POJO to be converted to text using the Serializer
registered with the
RestClient
.
HttpEntity
- Bypass Juneau serialization and pass HttpEntity directly to HttpClient.
RestCall
object that can be further tailored before executing the request
and getting the response as a parsed object.RestCallException
- If any authentication errors occurred.public RestCall doPut(Object url) throws RestCallException
doPut(Object, Object)
but don't specify the input yet.
You must call either RestCall.input(Object)
or RestCall.formData(String, Object)
to set the contents on the result object.
url
- The URL of the remote REST resource.
Can be any of the following: String
, URI
, URL
.RestCall
object that can be further tailored before executing the request and getting the response
as a parsed object.RestCallException
public RestCall doPost(Object url, Object o) throws RestCallException
POST
request against the specified URL.
doFormPost(Object, Object)
for application/x-www-form-urlencoded
form posts.
url
- The URL of the remote REST resource.
Can be any of the following: String
, URI
, URL
.o
- The object to serialize and transmit to the URL as the body of the request.
Can be of the following types:
Reader
- Raw contents of Reader
will be serialized to remote resource.
InputStream
- Raw contents of InputStream
will be serialized to remote resource.
Object
- POJO to be converted to text using the Serializer
registered with the RestClient
.
HttpEntity
- Bypass Juneau serialization and pass HttpEntity directly to HttpClient.
RestCall
object that can be further tailored before executing the request and getting the response
as a parsed object.RestCallException
- If any authentication errors occurred.public RestCall doPost(Object url) throws RestCallException
doPost(Object, Object)
but don't specify the input yet.
You must call either RestCall.input(Object)
or RestCall.formData(String, Object)
to set the
contents on the result object.
doFormPost(Object, Object)
for application/x-www-form-urlencoded
form posts.
url
- The URL of the remote REST resource.
Can be any of the following: String
, URI
, URL
.RestCall
object that can be further tailored before executing the request and getting the response
as a parsed object.RestCallException
public RestCall doDelete(Object url) throws RestCallException
DELETE
request against the specified URL.url
- The URL of the remote REST resource.
Can be any of the following: String
, URI
, URL
.RestCall
object that can be further tailored before executing the request and getting the response
as a parsed object.RestCallException
- If any authentication errors occurred.public RestCall doOptions(Object url) throws RestCallException
OPTIONS
request against the specified URL.url
- The URL of the remote REST resource.
Can be any of the following: String
, URI
, URL
.RestCall
object that can be further tailored before executing the request and getting the response
as a parsed object.RestCallException
- If any authentication errors occurred.public RestCall doFormPost(Object url, Object o) throws RestCallException
POST
request with a content type of application/x-www-form-urlencoded
against the specified URL.url
- The URL of the remote REST resource.
Can be any of the following: String
, URI
, URL
.o
- The object to serialize and transmit to the URL as the body of the request, serialized as a form post
using the UrlEncodingSerializer.DEFAULT
serializer.RestCall
object that can be further tailored before executing the request and getting the response
as a parsed object.RestCallException
- If any authentication errors occurred.public RestCall doCallback(String callString) throws RestCallException
This method is useful for performing callbacks when the target of a callback is passed in on an initial request, for example to signal when a long-running process has completed.
The call string can be any of the following formats:
The payload will always be sent using a simple StringEntity
.
callString
- The call string.RestCall
object that can be further tailored before executing the request and getting the response
as a parsed object.RestCallException
public RestCall doCall(HttpMethod method, Object url, Object content) throws RestCallException
method
- The HTTP method.url
- The URL of the remote REST resource.
Can be any of the following: String
, URI
, URL
.content
- The HTTP body content.
Can be of the following types:
Reader
- Raw contents of Reader
will be serialized to remote resource.
InputStream
- Raw contents of InputStream
will be serialized to remote resource.
Object
- POJO to be converted to text using the Serializer
registered with the
RestClient
.
HttpEntity
- Bypass Juneau serialization and pass HttpEntity directly to HttpClient.
NameValuePairs
- Converted to a URL-encoded FORM post.
HttpMethod.hasContent()
is RestCall
object that can be further tailored before executing the request and getting the response
as a parsed object.RestCallException
- If any authentication errors occurred.public RestCall doCall(String method, Object url, boolean hasContent) throws RestCallException
method
- The method name (e.g. url
- The URL of the remote REST resource.
Can be any of the following: String
, URI
, URL
.hasContent
- Boolean flag indicating if the specified request has content associated with it.RestCall
object that can be further tailored before executing the request and getting the response
as a parsed object.RestCallException
- If any authentication errors occurred.public <T> T getRemoteableProxy(Class<T> interfaceClass)
The URL to the REST interface is based on the following values:
@Remoteable.path()
annotation on the interface (remoteable-path
).
rootUrl
on the client (root-url
).
class-name
).
The URL calculation is as follows:
remoteable-path
- If remoteable path is absolute.
root-url/remoteable-path
- If remoteable path is relative and root-url has been specified.
root-url/class-name
- If remoteable path is not specified.
If the information is not available to resolve to an absolute URL, a RemoteableMetadataException
is thrown.
Examples:
RestClientBuilder.pooled()
method.
If you don't do this, you may end up seeing "Connection still allocated" exceptions.
interfaceClass
- The interface to create a proxy for.RemoteableMetadataException
- If the REST URI cannot be determined based on the information given.public <T> T getRemoteableProxy(Class<T> interfaceClass, Object restUrl)
getRemoteableProxy(Class)
except explicitly specifies the URL of the REST interface.interfaceClass
- The interface to create a proxy for.restUrl
- The URL of the REST interface.public <T> T getRemoteableProxy(Class<T> interfaceClass, Object restUrl, Serializer serializer, Parser parser)
getRemoteableProxy(Class, Object)
but allows you to override the serializer and parser used.interfaceClass
- The interface to create a proxy for.restUrl
- The URL of the REST interface.serializer
- The serializer used to serialize POJOs to the body of the HTTP request.parser
- The parser used to parse POJOs from the body of the HTTP response.Copyright © 2018 Apache. All rights reserved.