Skip to content

Commit 024ffd1

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 b949b90 commit 024ffd1

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
@@ -223,12 +223,16 @@ public S transactional(boolean handleMessageAdvice) {
223223
/**
224224
* Specify a {@link BiFunction} for customizing {@link Mono} replies via {@link ReactiveRequestHandlerAdvice}.
225225
* @param replyCustomizer the {@link BiFunction} to propagate into {@link ReactiveRequestHandlerAdvice}.
226+
* @param <T> inbound reply payload.
227+
* @param <V> outbound reply payload.
226228
* @return the spec.
227229
* @since 5.3
228230
* @see ReactiveRequestHandlerAdvice
229231
*/
230-
public S customizeMonoReply(BiFunction<Message<?>, Mono<?>, Publisher<?>> replyCustomizer) {
231-
return advice(new ReactiveRequestHandlerAdvice(replyCustomizer));
232+
@SuppressWarnings({ "unchecked", "rawtypes"})
233+
public <T, V> S customizeMonoReply(BiFunction<Message<?>, Mono<T>, Publisher<V>> replyCustomizer) {
234+
return advice(new ReactiveRequestHandlerAdvice(
235+
(BiFunction<Message<?>, Mono<?>, Publisher<?>>) (BiFunction) replyCustomizer));
232236
}
233237

234238
/**

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;
@@ -422,8 +423,12 @@ public IntegrationFlow webFluxFlowWithReplyPayloadToFlux() {
422423
.id("webFluxWithReplyPayloadToFlux")
423424
.customizeMonoReply(
424425
(message, mono) ->
425-
mono.timeout(Duration.ofMillis(100))
426-
.retry()));
426+
mono.
427+
timeout(Duration.ofMillis(100))
428+
.retry()
429+
.onErrorResume(
430+
WebClientResponseException.NotFound.class,
431+
ex -> Mono.just("Not Found"))));
427432
}
428433

429434
@Bean

0 commit comments

Comments
 (0)