Skip to main content

Interceptors

The RestCallInterceptor API provides a quick way of intercepting and manipulating requests and responses beyond the existing HttpRequestInterceptor and HttpResponseInterceptor APIs.

RestClient.Builderinterceptors(Object...)RestRequestinterceptors(RestCallInterceptor...)RestCallInterceptoronInit(RestRequest)onConnect(RestRequest,RestResponse)onClose(RestRequest,RestResponse)
Example
// Create a client with a customized interceptor.
RestClient client = RestClient
.create()
.interceptors(
new RestCallInterceptor() {

@Override
public void onInit(RestRequest req) throws Exception {
// Intercept immediately after RestRequest object is created and all headers/query/form-data has been
// set on the request from the client.
}

@Override
public void onConnect(RestRequest req, RestResponse res) throws Exception {
// Intercept immediately after an HTTP response has been received.
}

@Override
public void onClose(RestRequest req, RestResponse res) throws Exception {
// Intercept when the response body is consumed.
}
}
)
.build();

Annotation-Level Interceptors (Next-Gen Remote Proxies)​

When using the next-generation remote-proxy engine (RestClient.remote(...)), interceptors can also be declared declaratively on the proxy annotations rather than only on the builder. The interceptors() member is available on @Remote (interface-level) and on @RemoteOp / @RemoteGet / @RemotePost / @RemotePut / @RemoteDelete / @RemotePatch (method-level). Each class must implement RestCallInterceptor and provide a public no-arg constructor.

Example
@Remote(path="/api", interceptors=AuthInterceptor.class)   // applied to every method
public interface MyApi {

@RemoteGet(path="/audited", interceptors=AuditInterceptor.class) // plus this method only
Data getData();
}

The builder-configured, interface-level, and method-level interceptors are applied as a union in the order builder → interface → method (the method-level interceptor runs closest to the call).

The same annotations also carry the rest of the declarative call policy — per-call timeout, safe retries / retryNonIdempotent, and throwOnError. These are honored by the next-gen engine only; the classic getRemote(...) engine ignores them. See NG Remote Proxies for the full call-policy contract.