From 60a5ec87d03e4dea9a1a3550bec06797c889e66f Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Fri, 29 May 2015 23:23:52 +0200 Subject: [PATCH] Introduce alias for 'value' attribute in @Header This commit introduces 'name' as an alias for 'value' in @Header. Issue: SPR-11393 --- .../MethodJmsListenerEndpointTests.java | 17 +++++++++++++ .../messaging/handler/annotation/Header.java | 25 ++++++++++++++----- .../support/HeaderMethodArgumentResolver.java | 2 +- .../HeaderMethodArgumentResolverTests.java | 6 ++--- ...mpAnnotationMethodMessageHandlerTests.java | 4 +-- 5 files changed, 42 insertions(+), 12 deletions(-) diff --git a/spring-jms/src/test/java/org/springframework/jms/config/MethodJmsListenerEndpointTests.java b/spring-jms/src/test/java/org/springframework/jms/config/MethodJmsListenerEndpointTests.java index 090ecec307..05a306be8a 100644 --- a/spring-jms/src/test/java/org/springframework/jms/config/MethodJmsListenerEndpointTests.java +++ b/spring-jms/src/test/java/org/springframework/jms/config/MethodJmsListenerEndpointTests.java @@ -163,6 +163,17 @@ public class MethodJmsListenerEndpointTests { assertDefaultListenerMethodInvocation(); } + @Test + public void resolveCustomHeaderNameAndPayloadWithHeaderNameSet() throws JMSException { + MessagingMessageListenerAdapter listener = createDefaultInstance(String.class, int.class); + + Session session = mock(Session.class); + StubTextMessage message = createSimpleJmsTextMessage("my payload"); + message.setIntProperty("myCounter", 24); + listener.onMessage(message, session); + assertDefaultListenerMethodInvocation(); + } + @Test public void resolveHeaders() throws JMSException { MessagingMessageListenerAdapter listener = createDefaultInstance(String.class, Map.class); @@ -484,6 +495,12 @@ public class MethodJmsListenerEndpointTests { assertEquals("Wrong @Header resolution", 24, counter); } + public void resolveCustomHeaderNameAndPayloadWithHeaderNameSet(@Payload String content, @Header(name = "myCounter") int counter) { + invocations.put("resolveCustomHeaderNameAndPayloadWithHeaderNameSet", true); + assertEquals("Wrong @Payload resolution", "my payload", content); + assertEquals("Wrong @Header resolution", 24, counter); + } + public void resolveHeaders(String content, @Headers Map headers) { invocations.put("resolveHeaders", true); assertEquals("Wrong payload resolution", "my payload", content); diff --git a/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/Header.java b/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/Header.java index b0367339ab..d210a9ebaf 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/Header.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/Header.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 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. @@ -22,10 +22,13 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +import org.springframework.core.annotation.AliasFor; + /** * Annotation which indicates that a method parameter should be bound to a message header. * * @author Rossen Stoyanchev + * @author Sam Brannen * @since 4.0 */ @Target(ElementType.PARAMETER) @@ -34,20 +37,30 @@ import java.lang.annotation.Target; public @interface Header { /** - * The name of the request header to bind to. + * Alias for {@link #name}. */ + @AliasFor(attribute = "name") String value() default ""; + /** + * The name of the request header to bind to. + * @since 4.2 + */ + @AliasFor(attribute = "value") + String name() default ""; + /** * Whether the header is required. - *

Default is {@code true}, leading to an exception if the header missing. Switch this - * to {@code false} if you prefer a {@code null} in case of the header missing. + *

Default is {@code true}, leading to an exception if the header is + * missing. Switch this to {@code false} if you prefer a {@code null} + * value in case of a header missing. + * @see #defaultValue */ boolean required() default true; /** - * The default value to use as a fallback. Supplying a default value implicitly - * sets {@link #required} to {@code false}. + * The default value to use as a fallback. + *

Supplying a default value implicitly sets {@link #required} to {@code false}. */ String defaultValue() default ValueConstants.DEFAULT_NONE; diff --git a/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/HeaderMethodArgumentResolver.java b/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/HeaderMethodArgumentResolver.java index 8e885a194b..e32de6fa5e 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/HeaderMethodArgumentResolver.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/HeaderMethodArgumentResolver.java @@ -104,7 +104,7 @@ public class HeaderMethodArgumentResolver extends AbstractNamedValueMethodArgume private static class HeaderNamedValueInfo extends NamedValueInfo { private HeaderNamedValueInfo(Header annotation) { - super(annotation.value(), annotation.required(), annotation.defaultValue()); + super(annotation.name(), annotation.required(), annotation.defaultValue()); } } diff --git a/spring-messaging/src/test/java/org/springframework/messaging/handler/annotation/support/HeaderMethodArgumentResolverTests.java b/spring-messaging/src/test/java/org/springframework/messaging/handler/annotation/support/HeaderMethodArgumentResolverTests.java index b8b10da424..6985c9eb48 100644 --- a/spring-messaging/src/test/java/org/springframework/messaging/handler/annotation/support/HeaderMethodArgumentResolverTests.java +++ b/spring-messaging/src/test/java/org/springframework/messaging/handler/annotation/support/HeaderMethodArgumentResolverTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 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. @@ -139,8 +139,8 @@ public class HeaderMethodArgumentResolverTests { @SuppressWarnings("unused") private void handleMessage( @Header String param1, - @Header(value = "name", defaultValue = "bar") String param2, - @Header(value = "name", defaultValue="#{systemProperties.systemProperty}") String param3, + @Header(name = "name", defaultValue = "bar") String param2, + @Header(name = "name", defaultValue = "#{systemProperties.systemProperty}") String param3, String param4, @Header("nativeHeaders.param1") String nativeHeaderParam1) { } diff --git a/spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/SimpAnnotationMethodMessageHandlerTests.java b/spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/SimpAnnotationMethodMessageHandlerTests.java index 900b892659..a964639aeb 100644 --- a/spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/SimpAnnotationMethodMessageHandlerTests.java +++ b/spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/SimpAnnotationMethodMessageHandlerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 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. @@ -371,7 +371,7 @@ public class SimpAnnotationMethodMessageHandlerTests { } @MessageMapping("/optionalHeaders") - public void optionalHeaders(@Header(value="foo", required=false) String foo1, @Header(value="foo") Optional foo2) { + public void optionalHeaders(@Header(name="foo", required=false) String foo1, @Header("foo") Optional foo2) { this.method = "optionalHeaders"; this.arguments.put("foo1", foo1); this.arguments.put("foo2", (foo2.isPresent() ? foo2.get() : null));