Interface RequestId

  • All Superinterfaces:
    CharSequence

    public interface RequestId
    extends CharSequence
    An opaque identifier for the request.

    The request ID can then be obtained from the registry and used in response headers or logging. A request ID is always available.

    The value is determined by the RequestId.Generator present in the server registry. By default, a random UUID value is used.

    The following example demonstrates a custom request ID strategy using an incrementing long.

    
     import ratpack.core.handling.RequestId;
     import ratpack.core.http.client.ReceivedResponse;
     import ratpack.test.embed.EmbeddedApp;
     import static org.junit.jupiter.api.Assertions.*;
    
     public class Example {
       public static void main(String... args) throws Exception {
         EmbeddedApp.fromHandlers(chain -> chain
           .all(ctx -> {
             ctx.getResponse().getHeaders().add("X-Request-ID", ctx.get(RequestId.class));
             ctx.render("ok");
           })
         ).test(httpClient -> {
           ReceivedResponse response = httpClient.get();
           assertEquals("ok", response.getBody().getText());
    
           // Default request ID generator generates random UUIDs (i.e. 36 chars long)
           assertEquals(36, response.getHeaders().get("X-Request-ID").length());
         });
       }
     }
     

    Please note, adding an implementation to the request or context registries will have no effect. The generator is always obtained from the server registry.

    See Also:
    RequestId.Generator
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Interface Description
      static interface  RequestId.Generator
      Generates, or assigns, an ID for requests.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static com.google.common.reflect.TypeToken<RequestId> TYPE
      A type token for this type.
    • Field Detail

      • TYPE

        static final com.google.common.reflect.TypeToken<RequestId> TYPE
        A type token for this type.
        Since:
        1.1
    • Method Detail

      • of

        static RequestId of​(CharSequence requestId)
        Creates a new request ID from the given string.
        Parameters:
        requestId - the string of the request id
        Returns:
        a new request id