Skip to content

Commit f28819b

Browse files
artembilangaryrussell
authored andcommitted
Fix generics for customizeMonoReply()
Related to: https://stackoverflow.com/questions/68637283/how-to-customize-response-in-spring-integration-using-webflux-when-a-specific-er The function provided for the `ConsumerEndpointSpec.customizeMonoReply()` may convert incoming value to something else. With wildcards it cannot be compiled without casting. * Add `<T, V>` generic arg for the `customizeMonoReply()` to conform in and out types carrying. * Modify `WebFluxDslTests` to demonstrate the problem and confirm the fix **Cherry-pick to `5.4.x` & `5.3.x`**
1 parent 1a73151 commit f28819b

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

spring-integration-core/src/main/java/org/springframework/integration/dsl/ConsumerEndpointSpec.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,16 @@ public S transactional(boolean handleMessageAdvice) {
201201
/**
202202
* Specify a {@link BiFunction} for customizing {@link Mono} replies via {@link ReactiveRequestHandlerAdvice}.
203203
* @param replyCustomizer the {@link BiFunction} to propagate into {@link ReactiveRequestHandlerAdvice}.
204+
* @param <T> inbound reply payload.
205+
* @param <V> outbound reply payload.
204206
* @return the spec.
205207
* @since 5.3
206208
* @see ReactiveRequestHandlerAdvice
207209
*/
208-
public S customizeMonoReply(BiFunction<Message<?>, Mono<?>, Publisher<?>> replyCustomizer) {
209-
return advice(new ReactiveRequestHandlerAdvice(replyCustomizer));
210+
@SuppressWarnings({ "unchecked", "rawtypes"})
211+
public <T, V> S customizeMonoReply(BiFunction<Message<?>, Mono<T>, Publisher<V>> replyCustomizer) {
212+
return advice(new ReactiveRequestHandlerAdvice(
213+
(BiFunction<Message<?>, Mono<?>, Publisher<?>>) (BiFunction) replyCustomizer));
210214
}
211215

212216
/**

spring-integration-webflux/src/test/java/org/springframework/integration/webflux/dsl/WebFluxDslTests.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
import org.springframework.web.reactive.config.EnableWebFlux;
9292
import org.springframework.web.reactive.config.WebFluxConfigurer;
9393
import org.springframework.web.reactive.function.client.WebClient;
94+
import org.springframework.web.reactive.function.client.WebClientResponseException;
9495
import org.springframework.web.util.UriComponentsBuilder;
9596

9697
import reactor.core.publisher.Flux;
@@ -418,8 +419,12 @@ public IntegrationFlow webFluxFlowWithReplyPayloadToFlux() {
418419
.id("webFluxWithReplyPayloadToFlux")
419420
.customizeMonoReply(
420421
(message, mono) ->
421-
mono.timeout(Duration.ofMillis(100))
422-
.retry()));
422+
mono.
423+
timeout(Duration.ofMillis(100))
424+
.retry()
425+
.onErrorResume(
426+
WebClientResponseException.NotFound.class,
427+
ex -> Mono.just("Not Found"))));
423428
}
424429

425430
@Bean

0 commit comments

Comments
 (0)