How can ServerBuilder accept a list of decorators #5257
Answered
by
jrhee17
evgeny-mordyasov
asked this question in
Q&A
-
I have the following ServerBuilder with decorators. They are common to all REST services. If there are too many of them, it will look ugly. Is there any way to pass a list of decorators at once? I didn't find the serverBuilder.decorators(...) method. How it looks now:
How would I like to do:
|
Beta Was this translation helpful? Give feedback.
Answered by
jrhee17
Oct 20, 2023
Replies: 1 comment
-
For now, the only way I can think of is pre-merging all decorators: static Function<? super HttpService, ? extends HttpService> compose(
List<Function<? super HttpService, ? extends HttpService>> decorators) {
assert !decorators.isEmpty();
Function<? super HttpService, ? extends HttpService> decorated = decorators.get(0);
for (int i = 1; i < decorators.size(); i++) {
decorated = decorators.get(i).andThen(decorated);
}
return decorated;
} Having said this, I think it's reasonable to add some APIs which allow users to add multiple decorators. e.g. Server.builder()
.routeDecorator()
.path("/decorated2")
.build(LoggingService.newDecorator(), MetricCollectingService.newDecorator(...));
WebClient.builder()
.decorators(LoggingClient.newDecorator(), MetricCollectingClient.newDecorator(...)); Let me create an issue for this |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
evgeny-mordyasov
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For now, the only way I can think of is pre-merging all decorators:
Having said this, I think it's reasonable to add some APIs which allow users to add multiple decorators.
e.g.