Skip to content

Commit

Permalink
Add value() attribute to @payload
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoyanchev committed Oct 15, 2013
1 parent 70dfec2 commit bcfbd86
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@
@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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.springframework.messaging.support.converter.MessageConverter;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;


/**
Expand Down Expand Up @@ -69,6 +70,10 @@ public Object resolveArgument(MethodParameter parameter, Message<?> message) thr
}
}

if ((annot != null) && StringUtils.hasText(annot.value())) {
throw new IllegalStateException("@Payload SpEL expressions not supported by this resolver.");
}

return this.converter.fromMessage(message, targetClass);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class PayloadArgumentResolverTests {

private MethodParameter param;
private MethodParameter paramNotRequired;
private MethodParameter paramWithSpelExpression;


@Before
Expand All @@ -50,10 +51,11 @@ public void setup() throws Exception {
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);
}


Expand All @@ -75,11 +77,18 @@ public void resolveNotRequired() throws Exception {
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) {
}

}

0 comments on commit bcfbd86

Please sign in to comment.