Interface RequestInterceptor
- All Known Implementing Classes:
CorrelationIdInterceptor,HeaderInterceptor,LoggingInterceptor,MetricsInterceptor,RedactingLoggingInterceptor
RIP.addInterceptor(RequestInterceptor).
Both methods are observers, not a retry pipeline: beforeRequest can
add headers (or abort the call by throwing) before it goes out, and
afterResponse is notified once the response is back, but neither
can cause the request to be re-sent. A retry policy needs a different
mechanism than a passive interceptor and isn't supported here.
When several interceptors are registered, they run "onion"-style:
beforeRequest runs in registration order, but afterResponse runs
in the reverse order, so the first interceptor registered wraps every other
one and is the last to see the response - the same pairing used by OkHttp,
Servlet filters, and most middleware chains. Register an interceptor first
if it needs to bracket everything else's work (e.g. a timer measuring total
call overhead); register it last if it needs to sit closest to the actual
network call (e.g. a timer measuring only network latency).
-
Method Summary
Modifier and TypeMethodDescriptiondefault voidafterResponse(RequestContext context, int status, Object body) Called once the response is back.default voidbeforeRequest(RequestContext context) Called before the request is sent.default ShortCircuitResponseshortCircuit(RequestContext context) Called after every registered interceptor'sbeforeRequesthas run (in the same FIFO registration order asbeforeRequestitself) - returning a non-nullShortCircuitResponseskips the network call entirely and uses it as this call's response instead, decoded exactly as a real one would be (including throwingRestInPeaceHttpExceptionfor a non-2xx status).
-
Method Details
-
beforeRequest
Called before the request is sent. Can add headers viaRequestContext.addHeader(java.lang.String, java.lang.String), or abort the call by throwing.- Parameters:
context- the request being made
-
shortCircuit
Called after every registered interceptor'sbeforeRequesthas run (in the same FIFO registration order asbeforeRequestitself) - returning a non-nullShortCircuitResponseskips the network call entirely and uses it as this call's response instead, decoded exactly as a real one would be (including throwingRestInPeaceHttpExceptionfor a non-2xx status). The first interceptor (in that same FIFO order) to return non-nullwins; every interceptor'safterResponsestill runs afterward, exactly as it would for a real response. Enables a feature-flag bypass, a canary short-circuit, or a lightweight record/replay mode built on the interceptor chain instead of a real network dependency.A short-circuited response still goes through this client's own caching/retry configuration exactly like a real one would (e.g. it may get cached if it carries cacheable headers, or retried if its status matches
@Retry#retryOnStatus()- the latter simply re-invokes this method again instead of a real network call, so it's harmless if wasteful).- Parameters:
context- the request that would otherwise be sent- Returns:
- a synthetic response to short-circuit with, or
nullto let the request proceed normally
-
afterResponse
Called once the response is back.- Parameters:
context- the request that was madestatus- the HTTP response status codebody- the response body - aString, a deserialized object, ornullfor avoid-returning method
-