Skip to content

Commit 9362d0d

Browse files
committed
PayloadArgumentResolver does not insist on configured Validator anymore
Issue: SPR-12567 (cherry picked from commit ed0e2f4)
1 parent 37713ae commit 9362d0d

File tree

2 files changed

+42
-49
lines changed

2 files changed

+42
-49
lines changed

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

+31-21
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -16,13 +16,15 @@
1616

1717
package org.springframework.messaging.handler.annotation.support;
1818

19+
import java.lang.annotation.Annotation;
20+
1921
import org.springframework.core.MethodParameter;
2022
import org.springframework.core.annotation.AnnotationUtils;
2123
import org.springframework.messaging.Message;
2224
import org.springframework.messaging.converter.MessageConversionException;
25+
import org.springframework.messaging.converter.MessageConverter;
2326
import org.springframework.messaging.handler.annotation.Payload;
2427
import org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver;
25-
import org.springframework.messaging.converter.MessageConverter;
2628
import org.springframework.util.Assert;
2729
import org.springframework.util.ClassUtils;
2830
import org.springframework.util.ObjectUtils;
@@ -33,15 +35,13 @@
3335
import org.springframework.validation.SmartValidator;
3436
import org.springframework.validation.Validator;
3537

36-
import java.lang.annotation.Annotation;
37-
3838
/**
3939
* A resolver to extract and convert the payload of a message using a
4040
* {@link MessageConverter}. It also validates the payload using a
4141
* {@link Validator} if the argument is annotated with a Validation annotation.
4242
*
43-
* <p>This {@link HandlerMethodArgumentResolver} should be ordered last as it supports all
44-
* types and does not require the {@link Payload} annotation.
43+
* <p>This {@link HandlerMethodArgumentResolver} should be ordered last as it
44+
* supports all types and does not require the {@link Payload} annotation.
4545
*
4646
* @author Rossen Stoyanchev
4747
* @author Brian Clozel
@@ -55,9 +55,24 @@ public class PayloadArgumentResolver implements HandlerMethodArgumentResolver {
5555
private final Validator validator;
5656

5757

58+
/**
59+
* Create a new {@code PayloadArgumentResolver} with the given
60+
* {@link MessageConverter}.
61+
* @param messageConverter the MessageConverter to use (required)
62+
* @since 4.0.9
63+
*/
64+
public PayloadArgumentResolver(MessageConverter messageConverter) {
65+
this(messageConverter, null);
66+
}
67+
68+
/**
69+
* Create a new {@code PayloadArgumentResolver} with the given
70+
* {@link MessageConverter} and {@link Validator}.
71+
* @param messageConverter the MessageConverter to use (required)
72+
* @param validator the Validator to use (optional)
73+
*/
5874
public PayloadArgumentResolver(MessageConverter messageConverter, Validator validator) {
59-
Assert.notNull(messageConverter, "converter must not be null");
60-
Assert.notNull(validator, "validator must not be null");
75+
Assert.notNull(messageConverter, "MessageConverter must not be null");
6176
this.converter = messageConverter;
6277
this.validator = validator;
6378
}
@@ -70,14 +85,14 @@ public boolean supportsParameter(MethodParameter parameter) {
7085

7186
@Override
7287
public Object resolveArgument(MethodParameter param, Message<?> message) throws Exception {
73-
Payload annot = param.getParameterAnnotation(Payload.class);
74-
if ((annot != null) && StringUtils.hasText(annot.value())) {
88+
Payload ann = param.getParameterAnnotation(Payload.class);
89+
if (ann != null && StringUtils.hasText(ann.value())) {
7590
throw new IllegalStateException("@Payload SpEL expressions not supported by this resolver");
7691
}
7792

7893
Object payload = message.getPayload();
7994
if (isEmptyPayload(payload)) {
80-
if (annot == null || annot.required()) {
95+
if (ann == null || ann.required()) {
8196
String paramName = getParameterName(param);
8297
BindingResult bindingResult = new BeanPropertyBindingResult(payload, paramName);
8398
bindingResult.addError(new ObjectError(paramName, "@Payload param is required"));
@@ -106,7 +121,7 @@ public Object resolveArgument(MethodParameter param, Message<?> message) throws
106121

107122
private String getParameterName(MethodParameter param) {
108123
String paramName = param.getParameterName();
109-
return (paramName == null ? "Arg " + param.getParameterIndex() : paramName);
124+
return (paramName != null ? paramName : "Arg " + param.getParameterIndex());
110125
}
111126

112127
/**
@@ -132,26 +147,21 @@ protected void validate(Message<?> message, MethodParameter parameter, Object ta
132147
if (this.validator == null) {
133148
return;
134149
}
135-
136-
for (Annotation annot : parameter.getParameterAnnotations()) {
137-
if (annot.annotationType().getSimpleName().startsWith("Valid")) {
150+
for (Annotation ann : parameter.getParameterAnnotations()) {
151+
if (ann.annotationType().getSimpleName().startsWith("Valid")) {
152+
Object hints = AnnotationUtils.getValue(ann);
153+
Object[] validationHints = (hints instanceof Object[] ? (Object[]) hints : new Object[] {hints});
138154
BeanPropertyBindingResult bindingResult =
139155
new BeanPropertyBindingResult(target, getParameterName(parameter));
140-
141-
Object hints = AnnotationUtils.getValue(annot);
142-
Object[] validationHints = (hints instanceof Object[] ? (Object[]) hints : new Object[] {hints});
143-
144156
if (!ObjectUtils.isEmpty(validationHints) && this.validator instanceof SmartValidator) {
145157
((SmartValidator) this.validator).validate(target, bindingResult, validationHints);
146158
}
147159
else {
148160
this.validator.validate(target, bindingResult);
149161
}
150-
151162
if (bindingResult.hasErrors()) {
152163
throw new MethodArgumentNotValidException(message, parameter, bindingResult);
153164
}
154-
155165
break;
156166
}
157167
}

spring-messaging/src/main/java/org/springframework/messaging/simp/annotation/support/SimpAnnotationMethodMessageHandler.java

+11-28
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -64,7 +64,6 @@
6464
import org.springframework.util.Assert;
6565
import org.springframework.util.ClassUtils;
6666
import org.springframework.util.PathMatcher;
67-
import org.springframework.validation.Errors;
6867
import org.springframework.validation.Validator;
6968

7069
/**
@@ -181,7 +180,7 @@ public PathMatcher getPathMatcher() {
181180
* The configured Validator instance
182181
*/
183182
public Validator getValidator() {
184-
return validator;
183+
return this.validator;
185184
}
186185

187186
/**
@@ -253,8 +252,7 @@ protected List<HandlerMethodArgumentResolver> initArgumentResolvers() {
253252
resolvers.add(new MessageMethodArgumentResolver());
254253

255254
resolvers.addAll(getCustomArgumentResolvers());
256-
resolvers.add(new PayloadArgumentResolver(this.messageConverter,
257-
(this.validator != null ? this.validator : new NoOpValidator())));
255+
resolvers.add(new PayloadArgumentResolver(this.messageConverter, this.validator));
258256

259257
return resolvers;
260258
}
@@ -284,25 +282,23 @@ protected boolean isHandler(Class<?> beanType) {
284282

285283
@Override
286284
protected SimpMessageMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
287-
MessageMapping typeAnnot = AnnotationUtils.findAnnotation(handlerType, MessageMapping.class);
285+
MessageMapping typeAnnotation = AnnotationUtils.findAnnotation(handlerType, MessageMapping.class);
288286
MessageMapping messageAnnot = AnnotationUtils.findAnnotation(method, MessageMapping.class);
289287
if (messageAnnot != null) {
290288
SimpMessageMappingInfo result = createMessageMappingCondition(messageAnnot);
291-
if (typeAnnot != null) {
292-
result = createMessageMappingCondition(typeAnnot).combine(result);
289+
if (typeAnnotation != null) {
290+
result = createMessageMappingCondition(typeAnnotation).combine(result);
293291
}
294292
return result;
295293
}
296-
297-
SubscribeMapping subsribeAnnot = AnnotationUtils.findAnnotation(method, SubscribeMapping.class);
298-
if (subsribeAnnot != null) {
299-
SimpMessageMappingInfo result = createSubscribeCondition(subsribeAnnot);
300-
if (typeAnnot != null) {
301-
result = createMessageMappingCondition(typeAnnot).combine(result);
294+
SubscribeMapping subsribeAnnotation = AnnotationUtils.findAnnotation(method, SubscribeMapping.class);
295+
if (subsribeAnnotation != null) {
296+
SimpMessageMappingInfo result = createSubscribeCondition(subsribeAnnotation);
297+
if (typeAnnotation != null) {
298+
result = createMessageMappingCondition(typeAnnotation).combine(result);
302299
}
303300
return result;
304301
}
305-
306302
return null;
307303
}
308304

@@ -368,17 +364,4 @@ protected AbstractExceptionHandlerMethodResolver createExceptionHandlerMethodRes
368364
return new AnnotationExceptionHandlerMethodResolver(beanType);
369365
}
370366

371-
372-
private static final class NoOpValidator implements Validator {
373-
374-
@Override
375-
public boolean supports(Class<?> clazz) {
376-
return false;
377-
}
378-
379-
@Override
380-
public void validate(Object target, Errors errors) {
381-
}
382-
}
383-
384367
}

0 commit comments

Comments
 (0)