diff --git a/spring-integration-core/src/main/java/org/springframework/integration/config/MessagingGatewayRegistrar.java b/spring-integration-core/src/main/java/org/springframework/integration/config/MessagingGatewayRegistrar.java index 52e68113a3..11f4db8307 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/config/MessagingGatewayRegistrar.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/config/MessagingGatewayRegistrar.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2016 the original author or authors. + * Copyright 2014-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -114,29 +114,35 @@ public class MessagingGatewayRegistrar implements ImportBeanDefinitionRegistrar, : GatewayProxyFactoryBean.class); if (hasDefaultHeaders || hasDefaultPayloadExpression) { - BeanDefinitionBuilder methodMetadataBuilder = BeanDefinitionBuilder.genericBeanDefinition(GatewayMethodMetadata.class); + BeanDefinitionBuilder methodMetadataBuilder = + BeanDefinitionBuilder.genericBeanDefinition(GatewayMethodMetadata.class); + if (hasDefaultPayloadExpression) { methodMetadataBuilder.addPropertyValue("payloadExpression", defaultPayloadExpression); } - Map headerExpressions = new ManagedMap(); - for (Map header : defaultHeaders) { - String headerValue = (String) header.get("value"); - String headerExpression = (String) header.get("expression"); - boolean hasValue = StringUtils.hasText(headerValue); - if (hasValue == StringUtils.hasText(headerExpression)) { - throw new BeanDefinitionStoreException("exactly one of 'value' or 'expression' " + - "is required on a gateway's header."); + if (hasDefaultHeaders) { + Map headerExpressions = new ManagedMap(); + for (Map header : defaultHeaders) { + String headerValue = (String) header.get("value"); + String headerExpression = (String) header.get("expression"); + boolean hasValue = StringUtils.hasText(headerValue); + + if (hasValue == StringUtils.hasText(headerExpression)) { + throw new BeanDefinitionStoreException("exactly one of 'value' or 'expression' " + + "is required on a gateway's header."); + } + + BeanDefinition expressionDef = + new RootBeanDefinition(hasValue ? LiteralExpression.class : ExpressionFactoryBean.class); + expressionDef.getConstructorArgumentValues() + .addGenericArgumentValue(hasValue ? headerValue : headerExpression); + + headerExpressions.put((String) header.get("name"), expressionDef); } - - BeanDefinition expressionDef = - new RootBeanDefinition(hasValue ? LiteralExpression.class : ExpressionFactoryBean.class); - expressionDef.getConstructorArgumentValues() - .addGenericArgumentValue(hasValue ? headerValue : headerExpression); - - headerExpressions.put((String) header.get("name"), expressionDef); + methodMetadataBuilder.addPropertyValue("headerExpressions", headerExpressions); } - methodMetadataBuilder.addPropertyValue("headerExpressions", headerExpressions); + gatewayProxyBuilder.addPropertyValue("globalMethodMetadata", methodMetadataBuilder.getBeanDefinition()); } diff --git a/spring-integration-core/src/main/java/org/springframework/integration/gateway/GatewayProxyFactoryBean.java b/spring-integration-core/src/main/java/org/springframework/integration/gateway/GatewayProxyFactoryBean.java index 95b00c5f32..06f0e7125c 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/gateway/GatewayProxyFactoryBean.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/gateway/GatewayProxyFactoryBean.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -454,10 +454,15 @@ public class GatewayProxyFactoryBean extends AbstractEndpoint boolean hasPayloadExpression = method.isAnnotationPresent(org.springframework.integration.annotation.Payload.class) || method.isAnnotationPresent(Payload.class); - if (!hasPayloadExpression && this.methodMetadataMap != null) { + if (!hasPayloadExpression) { // check for the method metadata next - GatewayMethodMetadata metadata = this.methodMetadataMap.get(method.getName()); - hasPayloadExpression = (metadata != null) && StringUtils.hasText(metadata.getPayloadExpression()); + if (this.methodMetadataMap != null) { + GatewayMethodMetadata metadata = this.methodMetadataMap.get(method.getName()); + hasPayloadExpression = (metadata != null) && StringUtils.hasText(metadata.getPayloadExpression()); + } + else if (this.globalMethodMetadata != null) { + hasPayloadExpression = StringUtils.hasText(this.globalMethodMetadata.getPayloadExpression()); + } } if (paramCount == 0 && !hasPayloadExpression) { if (shouldReply) { diff --git a/spring-integration-core/src/test/java/org/springframework/integration/gateway/GatewayInterfaceTests-context.xml b/spring-integration-core/src/test/java/org/springframework/integration/gateway/GatewayInterfaceTests-context.xml index 951073d286..3ef67658c0 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/gateway/GatewayInterfaceTests-context.xml +++ b/spring-integration-core/src/test/java/org/springframework/integration/gateway/GatewayInterfaceTests-context.xml @@ -10,7 +10,7 @@ @@ -35,9 +35,14 @@ + + diff --git a/spring-integration-core/src/test/java/org/springframework/integration/gateway/GatewayInterfaceTests.java b/spring-integration-core/src/test/java/org/springframework/integration/gateway/GatewayInterfaceTests.java index 4947f6ab0c..44ca327bdb 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/gateway/GatewayInterfaceTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/gateway/GatewayInterfaceTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -68,6 +68,7 @@ import org.springframework.integration.config.EnableIntegration; import org.springframework.integration.configuration.EnableIntegrationTests.TestMessagingGateway; import org.springframework.integration.context.IntegrationContextUtils; import org.springframework.integration.context.IntegrationProperties; +import org.springframework.integration.handler.AbstractReplyProducingMessageHandler; import org.springframework.integration.handler.BridgeHandler; import org.springframework.integration.support.MessageBuilder; import org.springframework.integration.test.util.TestUtils; @@ -441,6 +442,26 @@ public class GatewayInterfaceTests { ((SubscribableChannel) this.errorChannel).unsubscribe(messageHandler); } + @Test + public void testGatewayWithNoArgsMethod() { + ConfigurableApplicationContext ac = + new ClassPathXmlApplicationContext("GatewayInterfaceTests-context.xml", getClass()); + + DirectChannel channel = ac.getBean("requestChannelBar", DirectChannel.class); + channel.subscribe(new AbstractReplyProducingMessageHandler() { + + @Override + protected Object handleRequestMessage(Message requestMessage) { + assertEquals("foo", requestMessage.getPayload()); + return "FOO"; + } + + }); + + NoArgumentsGateway noArgumentsGateway = ac.getBean(NoArgumentsGateway.class); + assertEquals("FOO", noArgumentsGateway.pullData()); + ac.close(); + } public interface Foo { @@ -469,6 +490,11 @@ public class GatewayInterfaceTests { void baz(String payload); } + public interface NoArgumentsGateway { + + String pullData(); + } + public static class BazMapper implements MethodArgsMessageMapper { @Override