Add value() attribute to @Payload
This commit is contained in:
@@ -38,6 +38,16 @@ import org.springframework.messaging.support.converter.MessageConverter;
|
|||||||
@Documented
|
@Documented
|
||||||
public @interface Payload {
|
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.
|
* Whether payload content is required.
|
||||||
* <p>
|
* <p>
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ import org.springframework.messaging.handler.method.HandlerMethodArgumentResolve
|
|||||||
import org.springframework.messaging.support.converter.MessageConverter;
|
import org.springframework.messaging.support.converter.MessageConverter;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.util.ClassUtils;
|
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);
|
return this.converter.fromMessage(message, targetClass);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ public class PayloadArgumentResolverTests {
|
|||||||
|
|
||||||
private MethodParameter param;
|
private MethodParameter param;
|
||||||
private MethodParameter paramNotRequired;
|
private MethodParameter paramNotRequired;
|
||||||
|
private MethodParameter paramWithSpelExpression;
|
||||||
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
@@ -50,10 +51,11 @@ public class PayloadArgumentResolverTests {
|
|||||||
this.resolver = new PayloadArgumentResolver(messageConverter );
|
this.resolver = new PayloadArgumentResolver(messageConverter );
|
||||||
|
|
||||||
Method method = PayloadArgumentResolverTests.class.getDeclaredMethod("handleMessage",
|
Method method = PayloadArgumentResolverTests.class.getDeclaredMethod("handleMessage",
|
||||||
String.class, String.class);
|
String.class, String.class, String.class);
|
||||||
|
|
||||||
this.param = new MethodParameter(method , 0);
|
this.param = new MethodParameter(method , 0);
|
||||||
this.paramNotRequired = new MethodParameter(method , 1);
|
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));
|
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")
|
@SuppressWarnings("unused")
|
||||||
private void handleMessage(
|
private void handleMessage(
|
||||||
@Payload String param,
|
@Payload String param,
|
||||||
@Payload(required=false) String paramNotRequired) {
|
@Payload(required=false) String paramNotRequired,
|
||||||
|
@Payload("foo.bar") String paramWithSpelExpression) {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user