Skip to content

Commit 16deb3c

Browse files
committed
Defensively register ReactiveReturnValueHandler for messaging methods
Closes gh-23207
1 parent a1eae42 commit 16deb3c

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/HandlerMethodReturnValueHandlerComposite.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,7 +26,6 @@
2626
import org.springframework.core.MethodParameter;
2727
import org.springframework.lang.Nullable;
2828
import org.springframework.messaging.Message;
29-
import org.springframework.util.Assert;
3029
import org.springframework.util.concurrent.ListenableFuture;
3130

3231
/**
@@ -139,9 +138,10 @@ public boolean isAsyncReturnValue(Object returnValue, MethodParameter returnType
139138
@Nullable
140139
public ListenableFuture<?> toListenableFuture(Object returnValue, MethodParameter returnType) {
141140
HandlerMethodReturnValueHandler handler = getReturnValueHandler(returnType);
142-
Assert.state(handler instanceof AsyncHandlerMethodReturnValueHandler,
143-
"AsyncHandlerMethodReturnValueHandler required");
144-
return ((AsyncHandlerMethodReturnValueHandler) handler).toListenableFuture(returnValue, returnType);
141+
if (handler instanceof AsyncHandlerMethodReturnValueHandler) {
142+
return ((AsyncHandlerMethodReturnValueHandler) handler).toListenableFuture(returnValue, returnType);
143+
}
144+
return null;
145145
}
146146

147147
}

spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/ReactiveReturnValueHandler.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.springframework.core.MethodParameter;
2222
import org.springframework.core.ReactiveAdapter;
2323
import org.springframework.core.ReactiveAdapterRegistry;
24-
import org.springframework.util.Assert;
2524
import org.springframework.util.concurrent.ListenableFuture;
2625
import org.springframework.util.concurrent.MonoToListenableFutureAdapter;
2726

@@ -60,8 +59,10 @@ public boolean isAsyncReturnValue(Object returnValue, MethodParameter returnType
6059
@Override
6160
public ListenableFuture<?> toListenableFuture(Object returnValue, MethodParameter returnType) {
6261
ReactiveAdapter adapter = this.adapterRegistry.getAdapter(returnType.getParameterType(), returnValue);
63-
Assert.state(adapter != null, () -> "No ReactiveAdapter found for " + returnType.getParameterType());
64-
return new MonoToListenableFutureAdapter<>(Mono.from(adapter.toPublisher(returnValue)));
62+
if (adapter != null) {
63+
return new MonoToListenableFutureAdapter<>(Mono.from(adapter.toPublisher(returnValue)));
64+
}
65+
return null;
6566
}
6667

6768
}

spring-messaging/src/main/java/org/springframework/messaging/simp/annotation/support/SimpAnnotationMethodMessageHandler.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -74,6 +74,7 @@
7474
import org.springframework.stereotype.Controller;
7575
import org.springframework.util.AntPathMatcher;
7676
import org.springframework.util.Assert;
77+
import org.springframework.util.ClassUtils;
7778
import org.springframework.util.CollectionUtils;
7879
import org.springframework.util.PathMatcher;
7980
import org.springframework.util.StringValueResolver;
@@ -93,6 +94,10 @@
9394
public class SimpAnnotationMethodMessageHandler extends AbstractMethodMessageHandler<SimpMessageMappingInfo>
9495
implements EmbeddedValueResolverAware, SmartLifecycle {
9596

97+
private static final boolean reactorPresent = ClassUtils.isPresent(
98+
"reactor.core.publisher.Flux", SimpAnnotationMethodMessageHandler.class.getClassLoader());
99+
100+
96101
private final SubscribableChannel clientInboundChannel;
97102

98103
private final SimpMessageSendingOperations clientMessagingTemplate;
@@ -328,7 +333,9 @@ protected List<? extends HandlerMethodReturnValueHandler> initReturnValueHandler
328333

329334
handlers.add(new ListenableFutureReturnValueHandler());
330335
handlers.add(new CompletableFutureReturnValueHandler());
331-
handlers.add(new ReactiveReturnValueHandler());
336+
if (reactorPresent) {
337+
handlers.add(new ReactiveReturnValueHandler());
338+
}
332339

333340
// Annotation-based return value types
334341

0 commit comments

Comments
 (0)