Я хочу реализовать универсальный веб-клиент, который автоматически добавляет идентификатор вызова в журналы запросов и ответов, поэтому я попробовал что-то вроде этого что выдает ошибку, поскольку "callId" находится вне контекста:
Код: Выделить всё
private WebClient createLoggingWebclient() {
return WebClient.builder()
.filter(addToCtx())
.filter(readCtx())
.baseUrl("http://localhost:8080")
.build();
}
private ExchangeFilterFunction readCtx() {
return ExchangeFilterFunction.ofResponseProcessor(
clientResponse ->
clientResponse.bodyToMono(String.class)
.flatMap(s ->
Mono.deferContextual(ctx -> {
// log response body and callId
log.info("callId: {}", (String)ctx.get(CALL_ID));
return Mono.just(clientResponse.mutate().body(s).build());
})
)
);
}
private ExchangeFilterFunction addToCtx() {
return ExchangeFilterFunction.ofRequestProcessor(
clientRequest ->
// log request body and callId
Mono.just(clientRequest).contextWrite(Context.of(CALL_ID, "123"))
);
}
Подробнее здесь: https://stackoverflow.com/questions/792 ... rfunctions