public interface RestClient
A new instance of this interface can be configured with it's fluent methods.
These methods return the same RestClient instance so the configuration can be performed
with method call chain. At the end of this chain the sendAndReceive(Class) method should be called
which executes the call and returns with the response.
The client instance has some default configuration.
method(HttpMethod) method.charset(Charset) method.mimeType(String) method.accept(String) method.Example REST API call:
MyRequest data = ...;
HttpResponse<MyResponse> response = restClient()
.url("http://myservice.xy/service")
.body(data)
.basicAuthorization("user", "pwd")
.sendAndReceive(MyResponse.class);
| Modifier and Type | Method and Description |
|---|---|
RestClient |
accept(String mimeType)
Sets the "Accept" header of the REST call.
|
RestClient |
authorization(String authorization)
Sets the "Authorization" header of the REST call.
|
RestClient |
basicAuthorization(String username,
String password)
Sets the "Authorization" header of the REST call.
|
RestClient |
bearerAuthorization(String token)
Sets the "Authorization" header of the REST call.
|
<T> RestClient |
body(T object)
Sets the body of the REST call.
|
RestClient |
charset(Charset charset)
Sets the charset of the REST call's content.
|
RestClient |
charset(String charsetName)
Sets the charset of the REST call's content.
|
RestClient |
header(String name,
String value)
Adds a HTTP header to the REST call.
|
RestClient |
method(HttpMethod method)
Sets the HTTP method of the REST call.
|
RestClient |
mimeType(String mimeType)
Sets the MIME type of the REST call's content.
|
<T> HttpResponse<T> |
sendAndReceive(Class<T> responseType)
Executes the REST call: sends the request and receives the response.
|
<T> HttpResponse<List<T>> |
sendAndReceiveList(Class<T> itemType)
Executes the REST call: sends the request and receives the response.
|
RestClient |
url(String url)
Sets the URL of the REST call.
|
RestClient |
url(URL url)
Sets the URL of the HTTP request.
|
RestClient |
withoutCertificateValidation()
Turns off SSL certificate validation.
|
RestClient url(String url)
http://localhost:8080/page,/page or service?param=x.If the URL is absolute, then it will be used by the REST client.
If the URL is relative, then it will be appended to the value of the ibello.url.base configuration property,
and the result URL will be used.
url - relative or absolute URLRestClient instanceNullPointerException - when the argument is nullRestClient url(URL url)
url - the URLRestClient instanceNullPointerException - when the argument is nullRestClient method(HttpMethod method)
method - the HTTP methodRestClient instanceRestClient mimeType(String mimeType)
mimeType - a valid MIME typeRestClient instanceRestClient charset(Charset charset)
charset - a java Charset instanceRestClient instanceRestClient charset(String charsetName)
charsetName - the name of the charsetRestClient instance<T> RestClient body(T object)
File instance, in this case a multipart attachment will be added to the body,InputStream instance, in this case the content of the stream will be the request body,String - it will be considered as a JSON value,T - the type of the body objectobject - the object (or JSON string) to be sentRestClient instanceRestClient header(String name, String value)
name - the name of the headervalue - the value of the headerRestClient instanceRestClient accept(String mimeType)
mimeType - a valid MIME typeRestClient instanceRestClient authorization(String authorization)
authorization - the value of the Authorization headerRestClient instanceRestClient basicAuthorization(String username, String password)
username - the authorization usernamepassword - the authorization passwordRestClient instanceRestClient bearerAuthorization(String token)
token - the authorization tokenRestClient instanceRestClient withoutCertificateValidation()
RestClient instance<T> HttpResponse<T> sendAndReceive(Class<T> responseType) throws IOException, IllegalArgumentException
responseType argument is String.class,
then the original JSON response will be returned in the result (without any transformation).T - type of the responseresponseType - class of the responseIOException - if the communication with the REST server failedIllegalArgumentException - if the response of the REST server was invalid<T> HttpResponse<List<T>> sendAndReceiveList(Class<T> itemType) throws IOException, IllegalArgumentException
T - type of the items in the response listitemType - class of the items in the response listIOException - if the communication with the REST server failedIllegalArgumentException - if the response of the REST server was invalidCreated by Kornél Simon, 2022