From bcfbd862c73db17d7bd8dfe5b669d1a8fc17bcbf Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Mon, 14 Oct 2013 22:16:13 -0400 Subject: [PATCH] Add value() attribute to @Payload --- .../messaging/handler/annotation/Payload.java | 10 ++++++++++ .../annotation/support/PayloadArgumentResolver.java | 5 +++++ .../support/PayloadArgumentResolverTests.java | 13 +++++++++++-- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/Payload.java b/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/Payload.java index dfc41afaab..af40fa889a 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/Payload.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/Payload.java @@ -38,6 +38,16 @@ import org.springframework.messaging.support.converter.MessageConverter; @Documented public @interface Payload { + /** + * A SpEL expression to be evaluated against the payload object as the root context. + * This attribute may or may not be supported depending on whether the message being + * handled contains a non-primitive Object as its payload or is in serialized form + * and requires message conversion. + *

+ * When processing STOMP over WebSocket messages this attribute is not supported. + */ + String value() default ""; + /** * Whether payload content is required. *

diff --git a/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/PayloadArgumentResolver.java b/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/PayloadArgumentResolver.java index ebff786da9..fa42fcf175 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/PayloadArgumentResolver.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/PayloadArgumentResolver.java @@ -23,6 +23,7 @@ import org.springframework.messaging.handler.method.HandlerMethodArgumentResolve import org.springframework.messaging.support.converter.MessageConverter; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; +import org.springframework.util.StringUtils; /** @@ -69,6 +70,10 @@ public class PayloadArgumentResolver implements HandlerMethodArgumentResolver { } } + if ((annot != null) && StringUtils.hasText(annot.value())) { + throw new IllegalStateException("@Payload SpEL expressions not supported by this resolver."); + } + return this.converter.fromMessage(message, targetClass); } diff --git a/spring-messaging/src/test/java/org/springframework/messaging/handler/annotation/support/PayloadArgumentResolverTests.java b/spring-messaging/src/test/java/org/springframework/messaging/handler/annotation/support/PayloadArgumentResolverTests.java index f14b6708b2..f64276d599 100644 --- a/spring-messaging/src/test/java/org/springframework/messaging/handler/annotation/support/PayloadArgumentResolverTests.java +++ b/spring-messaging/src/test/java/org/springframework/messaging/handler/annotation/support/PayloadArgumentResolverTests.java @@ -41,6 +41,7 @@ public class PayloadArgumentResolverTests { private MethodParameter param; private MethodParameter paramNotRequired; + private MethodParameter paramWithSpelExpression; @Before @@ -50,10 +51,11 @@ public class PayloadArgumentResolverTests { this.resolver = new PayloadArgumentResolver(messageConverter ); Method method = PayloadArgumentResolverTests.class.getDeclaredMethod("handleMessage", - String.class, String.class); + String.class, String.class, String.class); this.param = new MethodParameter(method , 0); this.paramNotRequired = new MethodParameter(method , 1); + this.paramWithSpelExpression = new MethodParameter(method , 2); } @@ -75,11 +77,18 @@ public class PayloadArgumentResolverTests { assertEquals("ABC", this.resolver.resolveArgument(this.paramNotRequired, notEmptyMessage)); } + @Test(expected=IllegalStateException.class) + public void resolveSpelExpressionNotSupported() throws Exception { + Message message = MessageBuilder.withPayload("ABC".getBytes()).build(); + this.resolver.resolveArgument(this.paramWithSpelExpression, message); + } + @SuppressWarnings("unused") private void handleMessage( @Payload String param, - @Payload(required=false) String paramNotRequired) { + @Payload(required=false) String paramNotRequired, + @Payload("foo.bar") String paramWithSpelExpression) { } }