Top Description Fields Constructors Methods
org.springframework.messaging.handler.annotation.reactive

public Class MessageMappingMessageHandler

extends AbstractMethodMessageHandler<CompositeMessageCondition>
implements EmbeddedValueResolverAware
Class Inheritance
All Implemented Interfaces
org.springframework.context.EmbeddedValueResolverAware, org.springframework.beans.factory.Aware
Known Direct Subclasses
org.springframework.messaging.rsocket.annotation.support.RSocketMessageHandler
Imports
java.lang.reflect.AnnotatedElement, .Method, java.util.ArrayList, .Arrays, .Collections, .Comparator, .LinkedHashSet, .List, .Map, .Set, java.util.function.Predicate, reactor.core.publisher.Mono, org.springframework.beans.factory.config.ConfigurableBeanFactory, org.springframework.context.ApplicationContext, .ConfigurableApplicationContext, .EmbeddedValueResolverAware, org.springframework.core.KotlinDetector, org.springframework.core.annotation.AnnotatedElementUtils, org.springframework.core.codec.Decoder, org.springframework.core.convert.ConversionService, org.springframework.format.support.DefaultFormattingConversionService, org.springframework.lang.Nullable, org.springframework.messaging.Message, org.springframework.messaging.handler.CompositeMessageCondition, .DestinationPatternsMessageCondition, .HandlerMethod, .MessagingAdviceBean, org.springframework.messaging.handler.annotation.MessageMapping, org.springframework.messaging.handler.annotation.support.AnnotationExceptionHandlerMethodResolver, org.springframework.messaging.handler.invocation.AbstractExceptionHandlerMethodResolver, org.springframework.messaging.handler.invocation.reactive.AbstractEncoderMethodReturnValueHandler, .AbstractMethodMessageHandler, .HandlerMethodArgumentResolver, .HandlerMethodReturnValueHandler, org.springframework.messaging.support.MessageHeaderAccessor, org.springframework.stereotype.Controller, org.springframework.util.AntPathMatcher, .Assert, .CollectionUtils, .RouteMatcher, .SimpleRouteMatcher, .StringValueResolver, org.springframework.validation.Validator

Extension of AbstractMethodMessageHandler for reactive, non-blocking handling of messages via @MessageMapping methods. By default, such methods are detected in @Controller Spring beans but that can be changed via setHandlerPredicate(Predicate).

Payloads for incoming messages are decoded through the configured setDecoders(List) decoders, with the help of PayloadMethodArgumentResolver.

There is no default handling for return values but setReturnValueHandlerConfigurer can be used to configure custom return value handlers. Subclasses may also override initReturnValueHandlers() to set up default return value handlers.

Author
Rossen Stoyanchev
Since
5.2
See Also
AbstractEncoderMethodReturnValueHandler

Field Summary

Modifier and TypeField and Description
private ConversionService
private final List<Decoder<?>>
private RouteMatcher
private Validator
private StringValueResolver
Inherited from org.springframework.messaging.handler.invocation.reactive.AbstractMethodMessageHandler:
logger

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
afterPropertiesSet()

Overrides org.springframework.messaging.handler.invocation.reactive.AbstractMethodMessageHandler.afterPropertiesSet.

Implements org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Invoked by the containing BeanFactory after it has set all bean properties and satisfied BeanFactoryAware, ApplicationContextAware etc.

This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.

protected AbstractExceptionHandlerMethodResolver
createExceptionMethodResolverFor(Class<?>
the class in which an exception occurred during handling
beanType
)

Implements abstract org.springframework.messaging.handler.invocation.reactive.AbstractMethodMessageHandler.createExceptionMethodResolverFor.

Create a concrete instance of AbstractExceptionHandlerMethodResolver that finds exception handling methods based on some criteria, e.g. based on the presence of @MessageExceptionHandler.

protected CompositeMessageCondition

Returns:

the condition, or null
getCondition
(AnnotatedElement
the element to check
element
)

Determine the mapping condition for the given annotated element.

public ConversionService
getConversionService()

Return the configured ConversionService.

public List<? extends Decoder<?>>
getDecoders()

Return the configured decoders.

protected RouteMatcher.Route
getDestination(Message<?> message)

Implements abstract org.springframework.messaging.handler.invocation.reactive.AbstractMethodMessageHandler.getDestination.

Extract the destination from the given message.

protected Set<String>
getDirectLookupMappings(CompositeMessageCondition mapping)

Implements abstract org.springframework.messaging.handler.invocation.reactive.AbstractMethodMessageHandler.getDirectLookupMappings.

Return String-based destinations for the given mapping, if any, that can be used to find matches with a direct lookup (i.e. non-patterns).

protected Comparator<CompositeMessageCondition>
getMappingComparator(Message<?>
the current Message
message
)

Implements abstract org.springframework.messaging.handler.invocation.reactive.AbstractMethodMessageHandler.getMappingComparator.

Return a comparator for sorting matching mappings.

protected CompositeMessageCondition
getMappingForMethod(Method
the method to check
method
,
Class<?>
the handler type, possibly a subtype of the method's declaring class
handlerType
)

Implements abstract org.springframework.messaging.handler.invocation.reactive.AbstractMethodMessageHandler.getMappingForMethod.

Obtain the mapping for the given method, if any.

protected CompositeMessageCondition
getMatchingMapping(CompositeMessageCondition
the mapping to get a match for
mapping
,
Message<?>
the message being handled
message
)

Implements abstract org.springframework.messaging.handler.invocation.reactive.AbstractMethodMessageHandler.getMatchingMapping.

Check if a mapping matches the current message and return a possibly new mapping with conditions relevant to the current request.

public RouteMatcher
getRouteMatcher()

Return the RouteMatcher used to map messages to handlers.

public Validator
getValidator()

Return the configured Validator instance.

protected Mono<Void>
protected List<? extends HandlerMethodArgumentResolver>
protected List<? extends HandlerMethodReturnValueHandler>
protected RouteMatcher

Returns:

the RouteMatcher (never null)
obtainRouteMatcher
()

Obtain the RouteMatcher for actual use.

protected String[]

Returns:

new array with the processed destinations or the same array
processDestinations
(String[]
the destinations
destinations
)

Resolve placeholders in the given destinations.

public void
registerMessagingAdvice(MessagingAdviceBean
the bean to check for @MessageExceptionHandler methods
bean
)

Use this method to register a MessagingAdviceBean that may contain globally applicable @MessageExceptionHandler methods.

public void
setConversionService(ConversionService
the conversion service to use
conversionService
)

Configure a ConversionService to use for type conversion of String based values, e.g. in destination variables or headers.

public void
setDecoders(List<? extends Decoder<?>> decoders)

Configure the decoders to use for incoming payloads.

public void
setEmbeddedValueResolver(StringValueResolver resolver)

Implements org.springframework.context.EmbeddedValueResolverAware.setEmbeddedValueResolver.

Set the StringValueResolver to use for resolving embedded definition values.

public void
setRouteMatcher(RouteMatcher routeMatcher)

Set the RouteMatcher to use for mapping messages to handlers based on the route patterns they're configured with.

public void
setValidator(Validator validator)

Set the Validator instance used for validating @Payload arguments.

Inherited from org.springframework.messaging.handler.invocation.reactive.AbstractMethodMessageHandler:
detectHandlerMethodsextendMappinggetApplicationContextgetArgumentResolverConfigurergetArgumentResolversgetBeanNamegetDestinationLookupgetHandlerMethodsgetHandlerPredicategetReactiveAdapterRegistrygetReturnValueHandlerConfigurerhandleMessagehandleNoMatchregisterExceptionHandlerAdviceregisterHandlerMethodsetApplicationContextsetArgumentResolverConfigurersetBeanNamesetHandlerPredicatesetHandlerssetReactiveAdapterRegistrysetReturnValueHandlerConfigurer

Field Detail

conversionServiceback to summary
private ConversionService conversionService
decodersback to summary
private final List<Decoder<?>> decoders
routeMatcherback to summary
private RouteMatcher routeMatcher
Annotations
@Nullable
validatorback to summary
private Validator validator
Annotations
@Nullable
valueResolverback to summary
private StringValueResolver valueResolver
Annotations
@Nullable

Constructor Detail

MessageMappingMessageHandlerback to summary
public MessageMappingMessageHandler()

Method Detail

afterPropertiesSetback to summary
public void afterPropertiesSet()

Overrides org.springframework.messaging.handler.invocation.reactive.AbstractMethodMessageHandler.afterPropertiesSet.

Implements org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Doc from org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Invoked by the containing BeanFactory after it has set all bean properties and satisfied BeanFactoryAware, ApplicationContextAware etc.

This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.

Annotations
@Override
createExceptionMethodResolverForback to summary
protected AbstractExceptionHandlerMethodResolver createExceptionMethodResolverFor(Class<?> beanType)

Implements abstract org.springframework.messaging.handler.invocation.reactive.AbstractMethodMessageHandler.createExceptionMethodResolverFor.

Doc from org.springframework.messaging.handler.invocation.reactive.AbstractMethodMessageHandler.createExceptionMethodResolverFor.

Create a concrete instance of AbstractExceptionHandlerMethodResolver that finds exception handling methods based on some criteria, e.g. based on the presence of @MessageExceptionHandler.

Parameters
beanType:Class<?>

the class in which an exception occurred during handling

Returns:AbstractExceptionHandlerMethodResolver

the resolver to use

Annotations
@Override
getConditionback to summary
protected CompositeMessageCondition getCondition(AnnotatedElement element)

Determine the mapping condition for the given annotated element.

Parameters
element:AnnotatedElement

the element to check

Returns:CompositeMessageCondition

the condition, or null

Annotations
@Nullable
getConversionServiceback to summary
public ConversionService getConversionService()

Return the configured ConversionService.

getDecodersback to summary
public List<? extends Decoder<?>> getDecoders()

Return the configured decoders.

getDestinationback to summary
protected RouteMatcher.Route getDestination(Message<?> message)

Implements abstract org.springframework.messaging.handler.invocation.reactive.AbstractMethodMessageHandler.getDestination.

Doc from org.springframework.messaging.handler.invocation.reactive.AbstractMethodMessageHandler.getDestination.

Extract the destination from the given message.

Annotations
@Override
getDirectLookupMappingsback to summary
protected Set<String> getDirectLookupMappings(CompositeMessageCondition mapping)

Implements abstract org.springframework.messaging.handler.invocation.reactive.AbstractMethodMessageHandler.getDirectLookupMappings.

Doc from org.springframework.messaging.handler.invocation.reactive.AbstractMethodMessageHandler.getDirectLookupMappings.

Return String-based destinations for the given mapping, if any, that can be used to find matches with a direct lookup (i.e. non-patterns).

Note

This is completely optional. The mapping metadata for a subclass may support neither direct lookups, nor String based destinations.

Annotations
@Override
getMappingComparatorback to summary
protected Comparator<CompositeMessageCondition> getMappingComparator(Message<?> message)

Implements abstract org.springframework.messaging.handler.invocation.reactive.AbstractMethodMessageHandler.getMappingComparator.

Doc from org.springframework.messaging.handler.invocation.reactive.AbstractMethodMessageHandler.getMappingComparator.

Return a comparator for sorting matching mappings. The returned comparator should sort 'better' matches higher.

Parameters
message:Message<?>

the current Message

Returns:Comparator<CompositeMessageCondition>

the comparator, never null

Annotations
@Override
getMappingForMethodback to summary
protected CompositeMessageCondition getMappingForMethod(Method method, Class<?> handlerType)

Implements abstract org.springframework.messaging.handler.invocation.reactive.AbstractMethodMessageHandler.getMappingForMethod.

Doc from org.springframework.messaging.handler.invocation.reactive.AbstractMethodMessageHandler.getMappingForMethod.

Obtain the mapping for the given method, if any.

Parameters
method:Method

the method to check

handlerType:Class<?>

the handler type, possibly a subtype of the method's declaring class

Returns:CompositeMessageCondition

the mapping, or null if the method is not mapped

Annotations
@Override
getMatchingMappingback to summary
protected CompositeMessageCondition getMatchingMapping(CompositeMessageCondition mapping, Message<?> message)

Implements abstract org.springframework.messaging.handler.invocation.reactive.AbstractMethodMessageHandler.getMatchingMapping.

Doc from org.springframework.messaging.handler.invocation.reactive.AbstractMethodMessageHandler.getMatchingMapping.

Check if a mapping matches the current message and return a possibly new mapping with conditions relevant to the current request.

Parameters
mapping:CompositeMessageCondition

the mapping to get a match for

message:Message<?>

the message being handled

Returns:CompositeMessageCondition

the match or null if there is no match

Annotations
@Override
getRouteMatcherback to summary
public RouteMatcher getRouteMatcher()

Return the RouteMatcher used to map messages to handlers. May be null before the component is initialized.

Annotations
@Nullable
getValidatorback to summary
public Validator getValidator()

Return the configured Validator instance.

Annotations
@Nullable
handleMatchback to summary
protected Mono<Void> handleMatch(CompositeMessageCondition mapping, HandlerMethod handlerMethod, Message<?> message)

Overrides org.springframework.messaging.handler.invocation.reactive.AbstractMethodMessageHandler.handleMatch.

Annotations
@Override
initArgumentResolversback to summary
protected List<? extends HandlerMethodArgumentResolver> initArgumentResolvers()

Implements abstract org.springframework.messaging.handler.invocation.reactive.AbstractMethodMessageHandler.initArgumentResolvers.

Doc from org.springframework.messaging.handler.invocation.reactive.AbstractMethodMessageHandler.initArgumentResolvers.

Return the list of argument resolvers to use.

Subclasses should also take into account custom argument types configured via setArgumentResolverConfigurer.

Annotations
@Override
initReturnValueHandlersback to summary
protected List<? extends HandlerMethodReturnValueHandler> initReturnValueHandlers()

Implements abstract org.springframework.messaging.handler.invocation.reactive.AbstractMethodMessageHandler.initReturnValueHandlers.

Doc from org.springframework.messaging.handler.invocation.reactive.AbstractMethodMessageHandler.initReturnValueHandlers.

Return the list of return value handlers to use.

Subclasses should also take into account custom return value types configured via setReturnValueHandlerConfigurer.

Annotations
@Override
obtainRouteMatcherback to summary
protected RouteMatcher obtainRouteMatcher()

Obtain the RouteMatcher for actual use.

Returns:RouteMatcher

the RouteMatcher (never null)

Exceptions
IllegalStateException:
in case of no RouteMatcher set
Since
5.0
processDestinationsback to summary
protected String[] processDestinations(String[] destinations)

Resolve placeholders in the given destinations.

Parameters
destinations:String[]

the destinations

Returns:String[]

new array with the processed destinations or the same array

registerMessagingAdviceback to summary
public void registerMessagingAdvice(MessagingAdviceBean bean)

Use this method to register a MessagingAdviceBean that may contain globally applicable @MessageExceptionHandler methods.

Note

spring-messaging does not depend on spring-web and therefore it is not possible to explicitly support the registration of a @ControllerAdvice bean. You can use the following adapter code to register @ControllerAdvice beans here:

ControllerAdviceBean.findAnnotatedBeans(context).forEach(bean ->
        messageHandler.registerMessagingAdvice(new ControllerAdviceWrapper(bean));

public class ControllerAdviceWrapper implements MessagingAdviceBean {
    private final ControllerAdviceBean delegate;
    // delegate all methods
}
Parameters
bean:MessagingAdviceBean

the bean to check for @MessageExceptionHandler methods

Since
5.3.5
setConversionServiceback to summary
public void setConversionService(ConversionService conversionService)

Configure a ConversionService to use for type conversion of String based values, e.g. in destination variables or headers.

By default DefaultFormattingConversionService is used.

Parameters
conversionService:ConversionService

the conversion service to use

setDecodersback to summary
public void setDecoders(List<? extends Decoder<?>> decoders)

Configure the decoders to use for incoming payloads.

setEmbeddedValueResolverback to summary
public void setEmbeddedValueResolver(StringValueResolver resolver)

Implements org.springframework.context.EmbeddedValueResolverAware.setEmbeddedValueResolver.

Doc from org.springframework.context.EmbeddedValueResolverAware.setEmbeddedValueResolver.

Set the StringValueResolver to use for resolving embedded definition values.

Annotations
@Override
setRouteMatcherback to summary
public void setRouteMatcher(RouteMatcher routeMatcher)

Set the RouteMatcher to use for mapping messages to handlers based on the route patterns they're configured with.

By default, SimpleRouteMatcher is used, backed by AntPathMatcher with "." as separator. For greater efficiency consider using the PathPatternRouteMatcher from spring-web instead.

setValidatorback to summary
public void setValidator(Validator validator)

Set the Validator instance used for validating @Payload arguments.

See Also
org.springframework.validation.annotation.Validated, PayloadMethodArgumentResolver