Class SubscriptionMethodReturnValueHandler
- java.lang.Object
- org.springframework.messaging.simp.annotation.support.SubscriptionMethodReturnValueHandler
- All Implemented Interfaces:
HandlerMethodReturnValueHandler
public class SubscriptionMethodReturnValueHandler extends Object implements HandlerMethodReturnValueHandler
HandlerMethodReturnValueHandler
for replying directly to a subscription. It is supported on methods annotated withSubscribeMapping
such that the return value is treated as a response to be sent directly back on the session. This allows a client to implement a request-response pattern and use it for example to obtain some data upon initialization.The value returned from the method is converted and turned into a
Message
that is then enriched with the sessionId, subscriptionId, and destination of the input message.Note: this default behavior for interpreting the return value from an
@SubscribeMapping
method can be overridden through use of theSendTo
orSendToUser
annotations in which case a message is prepared and sent to the broker instead.- Since:
- 4.0
- Author:
- Rossen Stoyanchev, Sebastien Deleuze
Constructor Summary
Constructors Constructor Description SubscriptionMethodReturnValueHandler(MessageSendingOperations<String> template)
Construct a new SubscriptionMethodReturnValueHandler.
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description MessageHeaderInitializer
getHeaderInitializer()
Return the configured header initializer.void
handleReturnValue(Object returnValue, MethodParameter returnType, Message<?> message)
Handle the given return value.void
setHeaderInitializer(MessageHeaderInitializer headerInitializer)
Configure aMessageHeaderInitializer
to apply to the headers of all messages sent to the client outbound channel.boolean
supportsReturnType(MethodParameter returnType)
Whether the given method return type is supported by this handler.
Constructor Detail
SubscriptionMethodReturnValueHandler
public SubscriptionMethodReturnValueHandler(MessageSendingOperations<String> template)
Construct a new SubscriptionMethodReturnValueHandler.- Parameters:
template
- a messaging template to send messages to, most likely the "clientOutboundChannel" (must not benull
)
Method Detail
setHeaderInitializer
public void setHeaderInitializer(@Nullable MessageHeaderInitializer headerInitializer)
Configure aMessageHeaderInitializer
to apply to the headers of all messages sent to the client outbound channel.By default this property is not set.
getHeaderInitializer
@Nullable public MessageHeaderInitializer getHeaderInitializer()
Return the configured header initializer.
supportsReturnType
public boolean supportsReturnType(MethodParameter returnType)
Description copied from interface:HandlerMethodReturnValueHandler
Whether the given method return type is supported by this handler.- Specified by:
supportsReturnType
in interfaceHandlerMethodReturnValueHandler
- Parameters:
returnType
- the method return type to check- Returns:
true
if this handler supports the supplied return type;false
otherwise
handleReturnValue
public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType, Message<?> message) throws Exception
Description copied from interface:HandlerMethodReturnValueHandler
Handle the given return value.- Specified by:
handleReturnValue
in interfaceHandlerMethodReturnValueHandler
- Parameters:
returnValue
- the value returned from the handler methodreturnType
- the type of the return value. This type must have previously been passed toHandlerMethodReturnValueHandler.supportsReturnType(org.springframework.core.MethodParameter)
and it must have returnedtrue
.message
- the message that caused this method to be called- Throws:
Exception
- if the return value handling results in an error