Class CorrelationIdInterceptor

java.lang.Object
com.shri.restinpeace.interceptor.CorrelationIdInterceptor
All Implemented Interfaces:
RequestInterceptor

public class CorrelationIdInterceptor extends Object implements RequestInterceptor
Pre-built interceptor that attaches a fresh correlation/request ID to every request - useful for tracing a call across service boundaries or correlating client-side logs with server-side logs. Defaults to a random UUID per call sent under the X-Request-Id header; both the header name and the ID generator are configurable.

The generated ID is also stashed on the RequestContext under ID_ATTRIBUTE, so another interceptor registered alongside this one (e.g. a logging or metrics interceptor) can read it back in beforeRequest/afterResponse to correlate its own output.

  • Field Details

  • Constructor Details

    • CorrelationIdInterceptor

      public CorrelationIdInterceptor()
      Generates a random UUID per call, sent under DEFAULT_HEADER_NAME.
    • CorrelationIdInterceptor

      public CorrelationIdInterceptor(String headerName)
      Generates a random UUID per call, sent under a custom header name.
      Parameters:
      headerName - the header to send the generated ID under
    • CorrelationIdInterceptor

      public CorrelationIdInterceptor(String headerName, Supplier<String> idGenerator)
      Uses a custom ID generator instead of a random UUID.
      Parameters:
      headerName - the header to send the generated ID under
      idGenerator - supplies a fresh ID for each request
  • Method Details