public interface RequestId extends CharSequence
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.handling.RequestId;
import ratpack.http.client.ReceivedResponse;
import ratpack.test.embed.EmbeddedApp;
import static org.junit.Assert.*;
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.
RequestId.Generator
Modifier and Type | Interface and Description |
---|---|
static interface |
RequestId.Generator
Generates, or assigns, an ID for requests.
|
Modifier and Type | Method and Description |
---|---|
static RequestId |
of(CharSequence requestId)
Creates a new request ID from the given string.
|
charAt, chars, codePoints, length, subSequence, toString
static RequestId of(CharSequence requestId)
requestId
- the string of the request id