'Helidon custom interceptor

I use helidon SE 1.4.4, how can add intercepor for all response. My route is:

return Routing.builder()
            .register(JsonSupport.create())
            .register("/api/files", health)           // Health at "/health"
            .register("/api/files", metrics)          // Metrics at "/metrics"
            .register("/api/files/storage", fileService)
            .register("/api/files", OpenAPISupport.create(config))
            .build();

I want use interceptor for add special headers in all my response.



Solution 1:[1]

Maybe one of the any methods on Routing.Builder might work for you.

https://helidon.io/docs/latest/apidocs/io/helidon/webserver/Routing.Builder.html

You would add what Helidon refers to as a Handler as opposed to what you called an interceptor, but from what you described that sounds like the behavior you want.

Hope that works.

Solution 2:[2]

This way we can add headers to all our response,

return Routing.builder().any((req, res) -> {

            res.addHeader("header1", "header1");
            res.addHeader("header2", "header2");
            req.next();
        })

                ...........
                ...........
                .register("/api/files", metrics) // Metrics at "/metrics"
                .register("/api/files/storage", fileService)
                .register("/api/files", OpenAPISupport.create(config))
                .build();

Response Headers

HTTP/1.1 200 OK
Content-Type: application/json
Date: Mon, 9 May 2022 21:35:40 +0530
header1: header1
header2: header2
connection: keep-alive
content-length: 46

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tim Quinn
Solution 2