From f1199623789618c7c7494d38288794204f4bce30 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 10 Dec 2015 00:30:50 +0100 Subject: [PATCH] SimpAnnotationMethodMessageHandler ignores empty marker annotations Issue: SPR-13704 --- .../SimpAnnotationMethodMessageHandler.java | 50 ++++++++++++------- 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/spring-messaging/src/main/java/org/springframework/messaging/simp/annotation/support/SimpAnnotationMethodMessageHandler.java b/spring-messaging/src/main/java/org/springframework/messaging/simp/annotation/support/SimpAnnotationMethodMessageHandler.java index aff2a293e4c..84b28e25d5e 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/simp/annotation/support/SimpAnnotationMethodMessageHandler.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/simp/annotation/support/SimpAnnotationMethodMessageHandler.java @@ -81,6 +81,7 @@ import org.springframework.validation.Validator; * * @author Rossen Stoyanchev * @author Brian Clozel + * @author Juergen Hoeller * @since 4.0 */ public class SimpAnnotationMethodMessageHandler extends AbstractMethodMessageHandler @@ -364,36 +365,47 @@ public class SimpAnnotationMethodMessageHandler extends AbstractMethodMessageHan @Override protected SimpMessageMappingInfo getMappingForMethod(Method method, Class handlerType) { - MessageMapping typeAnnotation = AnnotationUtils.findAnnotation(handlerType, MessageMapping.class); - MessageMapping messageAnnotation = AnnotationUtils.findAnnotation(method, MessageMapping.class); - if (messageAnnotation != null) { - SimpMessageMappingInfo result = createMessageMappingCondition(messageAnnotation); - if (typeAnnotation != null) { - result = createMessageMappingCondition(typeAnnotation).combine(result); + MessageMapping messageAnn = AnnotationUtils.findAnnotation(method, MessageMapping.class); + if (messageAnn != null) { + MessageMapping typeAnn = AnnotationUtils.findAnnotation(handlerType, MessageMapping.class); + // Only actually register it if there are destinations specified; + // otherwise @MessageMapping is just being used as a (meta-annotation) marker. + if (messageAnn.value().length > 0 || (typeAnn != null && typeAnn.value().length > 0)) { + SimpMessageMappingInfo result = createMessageMappingCondition(messageAnn.value()); + if (typeAnn != null) { + result = createMessageMappingCondition(typeAnn.value()).combine(result); + } + return result; } - return result; } - SubscribeMapping subscribeAnnotation = AnnotationUtils.findAnnotation(method, SubscribeMapping.class); - if (subscribeAnnotation != null) { - SimpMessageMappingInfo result = createSubscribeCondition(subscribeAnnotation); - if (typeAnnotation != null) { - result = createMessageMappingCondition(typeAnnotation).combine(result); + + SubscribeMapping subscribeAnn = AnnotationUtils.findAnnotation(method, SubscribeMapping.class); + if (subscribeAnn != null) { + MessageMapping typeAnn = AnnotationUtils.findAnnotation(handlerType, MessageMapping.class); + // Only actually register it if there are destinations specified; + // otherwise @SubscribeMapping is just being used as a (meta-annotation) marker. + if (subscribeAnn.value().length > 0 || (typeAnn != null && typeAnn.value().length > 0)) { + SimpMessageMappingInfo result = createSubscribeMappingCondition(subscribeAnn.value()); + if (typeAnn != null) { + result = createMessageMappingCondition(typeAnn.value()).combine(result); + } + return result; } - return result; } + return null; } - private SimpMessageMappingInfo createMessageMappingCondition(MessageMapping annotation) { - String[] destinations = resolveEmbeddedValuesInDestinations(annotation.value()); + private SimpMessageMappingInfo createMessageMappingCondition(String[] destinations) { + String[] resolvedDestinations = resolveEmbeddedValuesInDestinations(destinations); return new SimpMessageMappingInfo(SimpMessageTypeMessageCondition.MESSAGE, - new DestinationPatternsMessageCondition(destinations, this.pathMatcher)); + new DestinationPatternsMessageCondition(resolvedDestinations, this.pathMatcher)); } - private SimpMessageMappingInfo createSubscribeCondition(SubscribeMapping annotation) { - String[] destinations = resolveEmbeddedValuesInDestinations(annotation.value()); + private SimpMessageMappingInfo createSubscribeMappingCondition(String[] destinations) { + String[] resolvedDestinations = resolveEmbeddedValuesInDestinations(destinations); return new SimpMessageMappingInfo(SimpMessageTypeMessageCondition.SUBSCRIBE, - new DestinationPatternsMessageCondition(destinations, this.pathMatcher)); + new DestinationPatternsMessageCondition(resolvedDestinations, this.pathMatcher)); } /**