AMQP-784: Fix multi method @RabbitListener

JIRA: https://jira.spring.io/browse/AMQP-784

When looking for matching methods, we matched on no annotation or @Payload.

Match should have been no annotation(s) or not @Header.

__cherry-pick to 1.7.x__

(cherry picked from commit 1ddc74f)

* Replace `@NonNull` with the `@Validated` for Spring `4.3` support
This commit is contained in:
Gary Russell
2017-11-17 11:44:29 -05:00
committed by Artem Bilan
parent adf406c269
commit 4f4d0c57de
2 changed files with 7 additions and 5 deletions

View File

@@ -37,6 +37,7 @@ import org.springframework.expression.ParserContext;
import org.springframework.expression.common.TemplateParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.messaging.Message;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.handler.invocation.InvocableHandlerMethod;
@@ -189,11 +190,11 @@ public class DelegatingInvocableHandler {
protected boolean matchHandlerMethod(Class<? extends Object> payloadClass, InvocableHandlerMethod handler) {
Method method = handler.getMethod();
Annotation[][] parameterAnnotations = method.getParameterAnnotations();
// Single param; no annotation or @Payload
// Single param; no annotation or not @Header
if (parameterAnnotations.length == 1) {
MethodParameter methodParameter = new MethodParameter(method, 0);
if (methodParameter.getParameterAnnotations().length == 0
|| methodParameter.hasParameterAnnotation(Payload.class)) {
|| !methodParameter.hasParameterAnnotation(Header.class)) {
if (methodParameter.getParameterType().isAssignableFrom(payloadClass)) {
return true;
}
@@ -203,7 +204,7 @@ public class DelegatingInvocableHandler {
for (int i = 0; i < parameterAnnotations.length; i++) {
MethodParameter methodParameter = new MethodParameter(method, i);
if (methodParameter.getParameterAnnotations().length == 0
|| methodParameter.hasParameterAnnotation(Payload.class)) {
|| !methodParameter.hasParameterAnnotation(Header.class)) {
if (methodParameter.getParameterType().isAssignableFrom(payloadClass)) {
if (foundCandidate) {
throw new AmqpException("Ambiguous payload parameter for " + method.toGenericString());

View File

@@ -124,6 +124,7 @@ import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ErrorHandler;
import org.springframework.validation.annotation.Validated;
import com.rabbitmq.client.Channel;
@@ -1172,7 +1173,7 @@ public class EnableRabbitIntegrationTests {
@RabbitHandler
@SendTo("#{sendToRepliesBean}")
public String bar(Bar bar) {
public String bar(@Validated Bar bar) {
return "BAR: " + bar.field;
}
@@ -1182,7 +1183,7 @@ public class EnableRabbitIntegrationTests {
}
@RabbitHandler
public String qux(@Header("amqp_receivedRoutingKey") String rk, @Payload Qux qux) {
public String qux(@Header("amqp_receivedRoutingKey") String rk, @Validated @Payload Qux qux) {
return "QUX: " + qux.field + ": " + rk;
}