Interface RestCallInterceptor

All Known Implementing Classes:
McpAuthInterceptor

public interface RestCallInterceptor
Lifecycle interceptor for REST calls made by RestClient.

Registered on the client builder via RestClient.Builder.interceptors(RestCallInterceptor...). All callbacks have default no-op implementations so implementations only need to override the hooks they care about.

Example:

RestClient client = RestClient.builder() .transport(transport) .interceptors(new RestCallInterceptor() { @Override public void onConnect(RestRequest req, RestResponse res) { System.out.println("Response: " + res.getStatusCode()); } }) .build();

See Also:
Since:
9.2.1
  • Method Details

    • onInit

      default void onInit(RestRequest req) throws Exception
      Called before the transport request is built and sent.

      Use this hook to add headers, modify query parameters, or short-circuit the call.

      Parameters:
      req - The request being built. Never null.
      Throws:
      Exception - If the call should be aborted.
    • onConnect

      default void onConnect(RestRequest req, RestResponse res) throws Exception
      Called after a response has been received from the transport.

      Use this hook to inspect status codes, validate headers, or modify how the response is interpreted.

      Parameters:
      req - The request that was sent. Never null.
      res - The response received. Never null.
      Throws:
      Exception - If a post-connect error should be raised.
    • onClose

      default void onClose(RestRequest req, RestResponse res) throws Exception
      Called when the request is being closed (in the finally block of RestRequest.run()).

      This is called regardless of whether the call succeeded or failed, and is always invoked before the logger receives its entry. Use it for cleanup, metrics recording, or span/trace finalization.

      Parameters:
      req - The request. Never null.
      res - The response, or null if the transport failed before a response was received.
      Throws:
      Exception - If an error occurs during close handling.