INT-565 removed @MessageMapping and added @Payload

This commit is contained in:
Mark Fisher
2009-11-02 17:03:33 +00:00
parent af5d6e0e30
commit 543ec6f5f6
4 changed files with 40 additions and 29 deletions

View File

@@ -24,10 +24,12 @@ import java.lang.annotation.Target;
/**
* This annotation allows you to specify a SpEL expression indicating that a method
* parameter's value should be mapped from the result of expression processing.
* The annotated parameter must be of the required type.
* Example: void foo(@MessageMapping("headers.day") String arg) - will map the value of
* the 'day' header to 'arg'.
* parameter's value should be mapped from the payload of a Message. The expression
* will be evaluated against the payload object as the root context. The annotated
* parameter type must match or be convertible from the evaluation result.
* <p>
* Example: void foo(@Payload("city.name") String cityName) - will map the value of
* the 'name' property of the 'city' property of the payload object.
*
* @author Oleg Zhurakousky
* @since 2.0
@@ -35,8 +37,11 @@ import java.lang.annotation.Target;
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MessageMapping {
public @interface Payload {
String value();
/**
* Expression for matching against nested properties of the payload.
*/
String value() default "";
}

View File

@@ -40,7 +40,7 @@ import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.integration.annotation.Header;
import org.springframework.integration.annotation.Headers;
import org.springframework.integration.annotation.MessageMapping;
import org.springframework.integration.annotation.Payload;
import org.springframework.integration.core.Message;
import org.springframework.integration.message.InboundMessageMapper;
import org.springframework.integration.message.MessageBuilder;
@@ -168,8 +168,14 @@ public class ArgumentArrayMessageMapper implements InboundMessageMapper<Object[]
if (mappingAnnotation.annotationType().isAssignableFrom(Header.class)) {
value = this.mapHeaderThruAnnotation(mappingAnnotation, message, methodParameter, null)[1];
}
else if (mappingAnnotation.annotationType().isAssignableFrom(MessageMapping.class)) {
expressions = new String[] { (String) AnnotationUtils.getValue(mappingAnnotation) };
else if (mappingAnnotation.annotationType().isAssignableFrom(Payload.class)) {
String payloadExpression = ((Payload) mappingAnnotation).value();
if (payloadExpression.length() == 0) {
expressions = new String[] { "payload" };
}
else {
expressions = new String[] { "payload." + payloadExpression };
}
value = this.getValueFromMessageBasedOnEL(message, methodParameter.getParameterType(), true, expressions);
}
else if (mappingAnnotation.annotationType().isAssignableFrom(Headers.class)) {
@@ -186,7 +192,7 @@ public class ArgumentArrayMessageMapper implements InboundMessageMapper<Object[]
}
public Message<?> toMessage(Object[] arguments) {
Assert.notNull(arguments, "Can not map 'null' arguments to Message");
Assert.notNull(arguments, "cannot map null arguments to Message");
if (arguments.length > this.parameterList.size()) {
throw new IllegalArgumentException("Too many parameters provided for: " + method);
}
@@ -206,10 +212,10 @@ public class ArgumentArrayMessageMapper implements InboundMessageMapper<Object[]
Annotation match = null;
for (Annotation annotation : annotations) {
Class<? extends Annotation> type = annotation.annotationType();
if (type.equals(MessageMapping.class) || type.equals(Header.class) || type.equals(Headers.class)) {
if (type.equals(Payload.class) || type.equals(Header.class) || type.equals(Headers.class)) {
if (match != null) {
throw new IllegalArgumentException("at most one parameter annotation can be provided for message mapping, " +
"but found two [" + match.annotationType().getName() + "] and [" + annotation.annotationType().getName() + "]");
"but found two: [" + match.annotationType().getName() + "] and [" + annotation.annotationType().getName() + "]");
}
match = annotation;
}
@@ -250,9 +256,9 @@ public class ArgumentArrayMessageMapper implements InboundMessageMapper<Object[]
Object[] header = this.mapHeaderThruAnnotation(annotation, message, methodParam, argumentValue);
messageArgumentsMap.put((String) header[0], header[1]);
}
else if (annotation.annotationType().equals(MessageMapping.class)) {
else if (annotation.annotationType().equals(Payload.class)) {
// need to clarify what to do here
throw new IllegalArgumentException("@MessageMapping is not allowed when mapping from method to Message");
throw new IllegalArgumentException("@Payload is not allowed when mapping from method to Message");
}
}
Assert.isTrue(payloadExist, "Payload can not be determined from method: " + method);