Browse Source

Add value() attribute to @Payload

pull/387/head
Rossen Stoyanchev 11 years ago
parent
commit
bcfbd862c7
  1. 10
      spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/Payload.java
  2. 5
      spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/PayloadArgumentResolver.java
  3. 13
      spring-messaging/src/test/java/org/springframework/messaging/handler/annotation/support/PayloadArgumentResolverTests.java

10
spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/Payload.java

@ -38,6 +38,16 @@ import org.springframework.messaging.support.converter.MessageConverter; @@ -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.
* <p>
* When processing STOMP over WebSocket messages this attribute is not supported.
*/
String value() default "";
/**
* Whether payload content is required.
* <p>

5
spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/PayloadArgumentResolver.java

@ -23,6 +23,7 @@ import org.springframework.messaging.handler.method.HandlerMethodArgumentResolve @@ -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 { @@ -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);
}

13
spring-messaging/src/test/java/org/springframework/messaging/handler/annotation/support/PayloadArgumentResolverTests.java

@ -41,6 +41,7 @@ public class PayloadArgumentResolverTests { @@ -41,6 +41,7 @@ public class PayloadArgumentResolverTests {
private MethodParameter param;
private MethodParameter paramNotRequired;
private MethodParameter paramWithSpelExpression;
@Before
@ -50,10 +51,11 @@ public class PayloadArgumentResolverTests { @@ -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 { @@ -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) {
}
}

Loading…
Cancel
Save