Skip to content

Commit e2ecd93

Browse files
authored
feat: Support running an after-hook (#78)
1 parent 6588fd3 commit e2ecd93

12 files changed

Lines changed: 1870 additions & 28 deletions

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,33 @@ public class GetPromotionHandler implements RequestHandler {
343343
}
344344
```
345345

346+
### After-response hooks
347+
348+
Register code to run after the response has been sent. Hooks run on the request virtual thread,
349+
inside the library's request scope, with exceptions swallowed.
350+
351+
``` java
352+
OpenApiServer.builder()
353+
.spec(spec)
354+
.handlers(handlers)
355+
.afterResponseHook((req, resp) ->
356+
metrics.timer("http.request").record(req.operationId(), resp.status()))
357+
.build();
358+
```
359+
360+
Handlers can also queue per-request runnables:
361+
362+
``` java
363+
Map<String, RequestHandler> handlers = Map.of(
364+
"getThings", req -> {
365+
req.afterResponse(() -> auditLog.flush());
366+
return Response.ok(things);
367+
});
368+
```
369+
370+
Global hooks run first (registration order), then per-request runnables (FIFO). Pre-request
371+
failures (404, 405, validation) do not fire hooks. On the error path (when a handler throws), `Response#body()` is `null` and the bytes have already been streamed; use `Response#status()` to detect errors.
372+
346373
### End-to-end example
347374

348375
Gson on the classpath for request/response JSON, SnakeYAML on the classpath for the spec, one interceptor binding a request-scoped tenant + correlation id, one decorator stamping the correlation id on every response, one handler. No extra wiring.

0 commit comments

Comments
 (0)