Allow custom @Validated annotations for handler method parameters
Issue: SPR-12406
This commit is contained in:
@@ -16,6 +16,10 @@
|
||||
|
||||
package org.springframework.messaging.handler.annotation.support;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Locale;
|
||||
|
||||
@@ -72,10 +76,8 @@ public class PayloadArgumentResolverTests {
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
|
||||
this.resolver = new PayloadArgumentResolver(new StringMessageConverter(), testValidator());
|
||||
|
||||
payloadMethod = PayloadArgumentResolverTests.class.getDeclaredMethod("handleMessage",
|
||||
this.payloadMethod = PayloadArgumentResolverTests.class.getDeclaredMethod("handleMessage",
|
||||
String.class, String.class, Locale.class, String.class, String.class, String.class, String.class);
|
||||
|
||||
this.paramAnnotated = getMethodParameter(this.payloadMethod, 0);
|
||||
@@ -115,7 +117,6 @@ public class PayloadArgumentResolverTests {
|
||||
|
||||
@Test
|
||||
public void resolveNotRequired() throws Exception {
|
||||
|
||||
Message<?> emptyByteArrayMessage = MessageBuilder.withPayload(new byte[0]).build();
|
||||
assertNull(this.resolver.resolveArgument(this.paramAnnotatedNotRequired, emptyByteArrayMessage));
|
||||
|
||||
@@ -168,14 +169,12 @@ public class PayloadArgumentResolverTests {
|
||||
|
||||
@Test
|
||||
public void resolveNonAnnotatedParameter() throws Exception {
|
||||
|
||||
Message<?> notEmptyMessage = MessageBuilder.withPayload("ABC".getBytes()).build();
|
||||
assertEquals("ABC", this.resolver.resolveArgument(this.paramNotAnnotated, notEmptyMessage));
|
||||
|
||||
Message<?> emptyStringMessage = MessageBuilder.withPayload("").build();
|
||||
thrown.expect(MethodArgumentNotValidException.class);
|
||||
this.resolver.resolveArgument(this.paramValidated, emptyStringMessage);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -188,8 +187,8 @@ public class PayloadArgumentResolverTests {
|
||||
assertEquals("invalidValue", this.resolver.resolveArgument(this.paramValidatedNotAnnotated, message));
|
||||
}
|
||||
|
||||
private Validator testValidator() {
|
||||
|
||||
private Validator testValidator() {
|
||||
return new Validator() {
|
||||
@Override
|
||||
public boolean supports(Class<?> clazz) {
|
||||
@@ -216,9 +215,16 @@ public class PayloadArgumentResolverTests {
|
||||
@Payload(required=false) String paramNotRequired,
|
||||
@Payload(required=true) Locale nonConvertibleRequiredParam,
|
||||
@Payload("foo.bar") String paramWithSpelExpression,
|
||||
@Validated @Payload String validParam,
|
||||
@MyValid @Payload String validParam,
|
||||
@Validated String validParamNotAnnotated,
|
||||
String paramNotAnnotated) {
|
||||
}
|
||||
|
||||
|
||||
@Validated
|
||||
@Target({ElementType.PARAMETER})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface MyValid {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user