|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2016 the original author or authors. |
| 2 | + * Copyright 2002-2019 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.messaging.handler.annotation.support;
|
18 | 18 |
|
19 |
| -import java.lang.annotation.Annotation; |
20 |
| - |
21 |
| -import org.springframework.core.MethodParameter; |
22 |
| -import org.springframework.core.annotation.AnnotationUtils; |
23 | 19 | import org.springframework.lang.Nullable;
|
24 |
| -import org.springframework.messaging.Message; |
25 |
| -import org.springframework.messaging.converter.MessageConversionException; |
26 | 20 | import org.springframework.messaging.converter.MessageConverter;
|
27 |
| -import org.springframework.messaging.converter.SmartMessageConverter; |
28 |
| -import org.springframework.messaging.handler.annotation.Payload; |
29 |
| -import org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver; |
30 |
| -import org.springframework.util.Assert; |
31 |
| -import org.springframework.util.ClassUtils; |
32 |
| -import org.springframework.util.ObjectUtils; |
33 |
| -import org.springframework.util.StringUtils; |
34 |
| -import org.springframework.validation.BeanPropertyBindingResult; |
35 |
| -import org.springframework.validation.BindingResult; |
36 |
| -import org.springframework.validation.ObjectError; |
37 |
| -import org.springframework.validation.SmartValidator; |
38 | 21 | import org.springframework.validation.Validator;
|
39 |
| -import org.springframework.validation.annotation.Validated; |
40 | 22 |
|
41 | 23 | /**
|
42 | 24 | * A resolver to extract and convert the payload of a message using a
|
43 | 25 | * {@link MessageConverter}. It also validates the payload using a
|
44 | 26 | * {@link Validator} if the argument is annotated with a Validation annotation.
|
45 | 27 | *
|
46 |
| - * <p>This {@link HandlerMethodArgumentResolver} should be ordered last as it |
47 |
| - * supports all types and does not require the {@link Payload} annotation. |
48 |
| - * |
49 | 28 | * @author Rossen Stoyanchev
|
| 29 | + * @author Juergen Hoeller |
50 | 30 | * @author Brian Clozel
|
51 | 31 | * @author Stephane Nicoll
|
52 | 32 | * @since 4.0
|
| 33 | + * @deprecated as of 5.2, in favor of {@link PayloadMethodArgumentResolver} |
53 | 34 | */
|
54 |
| -public class PayloadArgumentResolver implements HandlerMethodArgumentResolver { |
55 |
| - |
56 |
| - private final MessageConverter converter; |
57 |
| - |
58 |
| - @Nullable |
59 |
| - private final Validator validator; |
60 |
| - |
61 |
| - private final boolean useDefaultResolution; |
62 |
| - |
| 35 | +@Deprecated |
| 36 | +public class PayloadArgumentResolver extends PayloadMethodArgumentResolver { |
63 | 37 |
|
64 | 38 | /**
|
65 | 39 | * Create a new {@code PayloadArgumentResolver} with the given
|
@@ -93,119 +67,8 @@ public PayloadArgumentResolver(MessageConverter messageConverter, @Nullable Vali
|
93 | 67 | public PayloadArgumentResolver(MessageConverter messageConverter, @Nullable Validator validator,
|
94 | 68 | boolean useDefaultResolution) {
|
95 | 69 |
|
96 |
| - Assert.notNull(messageConverter, "MessageConverter must not be null"); |
97 |
| - this.converter = messageConverter; |
98 |
| - this.validator = validator; |
99 |
| - this.useDefaultResolution = useDefaultResolution; |
100 |
| - } |
101 |
| - |
102 |
| - |
103 |
| - @Override |
104 |
| - public boolean supportsParameter(MethodParameter parameter) { |
105 |
| - return (parameter.hasParameterAnnotation(Payload.class) || this.useDefaultResolution); |
106 |
| - } |
107 |
| - |
108 |
| - @Override |
109 |
| - @Nullable |
110 |
| - public Object resolveArgument(MethodParameter parameter, Message<?> message) throws Exception { |
111 |
| - Payload ann = parameter.getParameterAnnotation(Payload.class); |
112 |
| - if (ann != null && StringUtils.hasText(ann.expression())) { |
113 |
| - throw new IllegalStateException("@Payload SpEL expressions not supported by this resolver"); |
114 |
| - } |
115 | 70 |
|
116 |
| - Object payload = message.getPayload(); |
117 |
| - if (isEmptyPayload(payload)) { |
118 |
| - if (ann == null || ann.required()) { |
119 |
| - String paramName = getParameterName(parameter); |
120 |
| - BindingResult bindingResult = new BeanPropertyBindingResult(payload, paramName); |
121 |
| - bindingResult.addError(new ObjectError(paramName, "Payload value must not be empty")); |
122 |
| - throw new MethodArgumentNotValidException(message, parameter, bindingResult); |
123 |
| - } |
124 |
| - else { |
125 |
| - return null; |
126 |
| - } |
127 |
| - } |
128 |
| - |
129 |
| - Class<?> targetClass = parameter.getParameterType(); |
130 |
| - Class<?> payloadClass = payload.getClass(); |
131 |
| - if (ClassUtils.isAssignable(targetClass, payloadClass)) { |
132 |
| - validate(message, parameter, payload); |
133 |
| - return payload; |
134 |
| - } |
135 |
| - else { |
136 |
| - if (this.converter instanceof SmartMessageConverter) { |
137 |
| - SmartMessageConverter smartConverter = (SmartMessageConverter) this.converter; |
138 |
| - payload = smartConverter.fromMessage(message, targetClass, parameter); |
139 |
| - } |
140 |
| - else { |
141 |
| - payload = this.converter.fromMessage(message, targetClass); |
142 |
| - } |
143 |
| - if (payload == null) { |
144 |
| - throw new MessageConversionException(message, "Cannot convert from [" + |
145 |
| - payloadClass.getName() + "] to [" + targetClass.getName() + "] for " + message); |
146 |
| - } |
147 |
| - validate(message, parameter, payload); |
148 |
| - return payload; |
149 |
| - } |
150 |
| - } |
151 |
| - |
152 |
| - private String getParameterName(MethodParameter param) { |
153 |
| - String paramName = param.getParameterName(); |
154 |
| - return (paramName != null ? paramName : "Arg " + param.getParameterIndex()); |
155 |
| - } |
156 |
| - |
157 |
| - /** |
158 |
| - * Specify if the given {@code payload} is empty. |
159 |
| - * @param payload the payload to check (can be {@code null}) |
160 |
| - */ |
161 |
| - protected boolean isEmptyPayload(@Nullable Object payload) { |
162 |
| - if (payload == null) { |
163 |
| - return true; |
164 |
| - } |
165 |
| - else if (payload instanceof byte[]) { |
166 |
| - return ((byte[]) payload).length == 0; |
167 |
| - } |
168 |
| - else if (payload instanceof String) { |
169 |
| - return !StringUtils.hasText((String) payload); |
170 |
| - } |
171 |
| - else { |
172 |
| - return false; |
173 |
| - } |
174 |
| - } |
175 |
| - |
176 |
| - /** |
177 |
| - * Validate the payload if applicable. |
178 |
| - * <p>The default implementation checks for {@code @javax.validation.Valid}, |
179 |
| - * Spring's {@link org.springframework.validation.annotation.Validated}, |
180 |
| - * and custom annotations whose name starts with "Valid". |
181 |
| - * @param message the currently processed message |
182 |
| - * @param parameter the method parameter |
183 |
| - * @param target the target payload object |
184 |
| - * @throws MethodArgumentNotValidException in case of binding errors |
185 |
| - */ |
186 |
| - protected void validate(Message<?> message, MethodParameter parameter, Object target) { |
187 |
| - if (this.validator == null) { |
188 |
| - return; |
189 |
| - } |
190 |
| - for (Annotation ann : parameter.getParameterAnnotations()) { |
191 |
| - Validated validatedAnn = AnnotationUtils.getAnnotation(ann, Validated.class); |
192 |
| - if (validatedAnn != null || ann.annotationType().getSimpleName().startsWith("Valid")) { |
193 |
| - Object hints = (validatedAnn != null ? validatedAnn.value() : AnnotationUtils.getValue(ann)); |
194 |
| - Object[] validationHints = (hints instanceof Object[] ? (Object[]) hints : new Object[] {hints}); |
195 |
| - BeanPropertyBindingResult bindingResult = |
196 |
| - new BeanPropertyBindingResult(target, getParameterName(parameter)); |
197 |
| - if (!ObjectUtils.isEmpty(validationHints) && this.validator instanceof SmartValidator) { |
198 |
| - ((SmartValidator) this.validator).validate(target, bindingResult, validationHints); |
199 |
| - } |
200 |
| - else { |
201 |
| - this.validator.validate(target, bindingResult); |
202 |
| - } |
203 |
| - if (bindingResult.hasErrors()) { |
204 |
| - throw new MethodArgumentNotValidException(message, parameter, bindingResult); |
205 |
| - } |
206 |
| - break; |
207 |
| - } |
208 |
| - } |
| 71 | + super(messageConverter, validator, useDefaultResolution); |
209 | 72 | }
|
210 | 73 |
|
211 | 74 | }
|
0 commit comments