Browse Source

Introduce alias for 'value' attribute in @Header

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

Issue: SPR-11393
pull/811/head
Sam Brannen 10 years ago
parent
commit
60a5ec87d0
  1. 17
      spring-jms/src/test/java/org/springframework/jms/config/MethodJmsListenerEndpointTests.java
  2. 25
      spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/Header.java
  3. 2
      spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/HeaderMethodArgumentResolver.java
  4. 6
      spring-messaging/src/test/java/org/springframework/messaging/handler/annotation/support/HeaderMethodArgumentResolverTests.java
  5. 4
      spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/SimpAnnotationMethodMessageHandlerTests.java

17
spring-jms/src/test/java/org/springframework/jms/config/MethodJmsListenerEndpointTests.java

@ -163,6 +163,17 @@ public class MethodJmsListenerEndpointTests { @@ -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 { @@ -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<String, Object> headers) {
invocations.put("resolveHeaders", true);
assertEquals("Wrong payload resolution", "my payload", content);

25
spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/Header.java

@ -1,5 +1,5 @@ @@ -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; @@ -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; @@ -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.
* <p>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.
* <p>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.
* <p>Supplying a default value implicitly sets {@link #required} to {@code false}.
*/
String defaultValue() default ValueConstants.DEFAULT_NONE;

2
spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/HeaderMethodArgumentResolver.java

@ -104,7 +104,7 @@ public class HeaderMethodArgumentResolver extends AbstractNamedValueMethodArgume @@ -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());
}
}

6
spring-messaging/src/test/java/org/springframework/messaging/handler/annotation/support/HeaderMethodArgumentResolverTests.java

@ -1,5 +1,5 @@ @@ -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 { @@ -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) {
}

4
spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/SimpAnnotationMethodMessageHandlerTests.java

@ -1,5 +1,5 @@ @@ -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 { @@ -371,7 +371,7 @@ public class SimpAnnotationMethodMessageHandlerTests {
}
@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.arguments.put("foo1", foo1);
this.arguments.put("foo2", (foo2.isPresent() ? foo2.get() : null));

Loading…
Cancel
Save