Introduce alias for 'value' attribute in @Header

This commit introduces 'name' as an alias for 'value' in @Header.

Issue: SPR-11393
This commit is contained in:
Sam Brannen 2015-05-29 23:23:52 +02:00
parent 35c3e7c0f3
commit 60a5ec87d0
5 changed files with 42 additions and 12 deletions

View File

@ -163,6 +163,17 @@ public class MethodJmsListenerEndpointTests {
assertDefaultListenerMethodInvocation(); 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 @Test
public void resolveHeaders() throws JMSException { public void resolveHeaders() throws JMSException {
MessagingMessageListenerAdapter listener = createDefaultInstance(String.class, Map.class); MessagingMessageListenerAdapter listener = createDefaultInstance(String.class, Map.class);
@ -484,6 +495,12 @@ public class MethodJmsListenerEndpointTests {
assertEquals("Wrong @Header resolution", 24, counter); 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<String, Object> headers) { public void resolveHeaders(String content, @Headers Map<String, Object> headers) {
invocations.put("resolveHeaders", true); invocations.put("resolveHeaders", true);
assertEquals("Wrong payload resolution", "my payload", content); assertEquals("Wrong payload resolution", "my payload", content);

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.RetentionPolicy;
import java.lang.annotation.Target; 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. * Annotation which indicates that a method parameter should be bound to a message header.
* *
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @author Sam Brannen
* @since 4.0 * @since 4.0
*/ */
@Target(ElementType.PARAMETER) @Target(ElementType.PARAMETER)
@ -34,20 +37,30 @@ import java.lang.annotation.Target;
public @interface Header { public @interface Header {
/** /**
* The name of the request header to bind to. * Alias for {@link #name}.
*/ */
@AliasFor(attribute = "name")
String value() default ""; 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. * Whether the header is required.
* <p>Default is {@code true}, leading to an exception if the header missing. Switch this * <p>Default is {@code true}, leading to an exception if the header is
* to {@code false} if you prefer a {@code null} in case of the header missing. * 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; boolean required() default true;
/** /**
* The default value to use as a fallback. Supplying a default value implicitly * The default value to use as a fallback.
* sets {@link #required} to {@code false}. * <p>Supplying a default value implicitly sets {@link #required} to {@code false}.
*/ */
String defaultValue() default ValueConstants.DEFAULT_NONE; String defaultValue() default ValueConstants.DEFAULT_NONE;

View File

@ -104,7 +104,7 @@ public class HeaderMethodArgumentResolver extends AbstractNamedValueMethodArgume
private static class HeaderNamedValueInfo extends NamedValueInfo { private static class HeaderNamedValueInfo extends NamedValueInfo {
private HeaderNamedValueInfo(Header annotation) { private HeaderNamedValueInfo(Header annotation) {
super(annotation.value(), annotation.required(), annotation.defaultValue()); super(annotation.name(), annotation.required(), annotation.defaultValue());
} }
} }

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -139,8 +139,8 @@ public class HeaderMethodArgumentResolverTests {
@SuppressWarnings("unused") @SuppressWarnings("unused")
private void handleMessage( private void handleMessage(
@Header String param1, @Header String param1,
@Header(value = "name", defaultValue = "bar") String param2, @Header(name = "name", defaultValue = "bar") String param2,
@Header(value = "name", defaultValue="#{systemProperties.systemProperty}") String param3, @Header(name = "name", defaultValue = "#{systemProperties.systemProperty}") String param3,
String param4, String param4,
@Header("nativeHeaders.param1") String nativeHeaderParam1) { @Header("nativeHeaders.param1") String nativeHeaderParam1) {
} }

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -371,7 +371,7 @@ public class SimpAnnotationMethodMessageHandlerTests {
} }
@MessageMapping("/optionalHeaders") @MessageMapping("/optionalHeaders")
public void optionalHeaders(@Header(value="foo", required=false) String foo1, @Header(value="foo") Optional<String> foo2) { public void optionalHeaders(@Header(name="foo", required=false) String foo1, @Header("foo") Optional<String> foo2) {
this.method = "optionalHeaders"; this.method = "optionalHeaders";
this.arguments.put("foo1", foo1); this.arguments.put("foo1", foo1);
this.arguments.put("foo2", (foo2.isPresent() ? foo2.get() : null)); this.arguments.put("foo2", (foo2.isPresent() ? foo2.get() : null));