public interface RequestId extends CharSequence
The request ID can then be obtained from the registry and used in response headers or logging.
Out of the box, a request ID is available for all requests.
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());
});
}
}
The default ID RequestId.Generator
uses random UUIDs.
To use an alternative strategy, provide an implementation of the RequestId.Generator
interface in the application's registry.
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 a unique request ID.
|
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