Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,33 @@ public class GetPromotionHandler implements RequestHandler {
}
```

### After-response hooks

Register code to run after the response has been sent. Hooks run on the request virtual thread,
inside the library's request scope, with exceptions swallowed.

``` java
OpenApiServer.builder()
.spec(spec)
.handlers(handlers)
.afterResponseHook((req, resp) ->
metrics.timer("http.request").record(req.operationId(), resp.status()))
.build();
```

Handlers can also queue per-request runnables:

``` java
Map<String, RequestHandler> handlers = Map.of(
"getThings", req -> {
req.afterResponse(() -> auditLog.flush());
return Response.ok(things);
});
```

Global hooks run first (registration order), then per-request runnables (FIFO). Pre-request
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.

### End-to-end example

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.
Expand Down
Loading
Loading