24
24
import org .apache .commons .logging .Log ;
25
25
import org .apache .commons .logging .LogFactory ;
26
26
27
- import org .springframework .core .MethodParameter ;
28
27
import org .springframework .messaging .Message ;
29
28
import org .springframework .messaging .MessageHeaders ;
30
29
import org .springframework .messaging .support .MessageBuilder ;
40
39
*
41
40
* @author Rossen Stoyanchev
42
41
* @author Sebastien Deleuze
42
+ * @author Juergen Hoeller
43
43
* @since 4.0
44
44
*/
45
45
public abstract class AbstractMessageConverter implements MessageConverter {
46
46
47
- /**
48
- * Name of the header that can be set to provide further information
49
- * ({@link MethodParameter} instance) about the origin of the payload (for
50
- * {@link #toMessage(Object, MessageHeaders)}) or about the target of the payload
51
- * ({@link #fromMessage(Message, Class)}).
52
- * @since 4.2
53
- */
54
- public static final String METHOD_PARAMETER_HINT_HEADER = "methodParameterHint" ;
55
-
56
-
57
47
protected final Log logger = LogFactory .getLog (getClass ());
58
48
59
49
private final List <MimeType > supportedMimeTypes ;
@@ -174,10 +164,27 @@ protected MimeType getDefaultContentType(Object payload) {
174
164
175
165
@ Override
176
166
public final Object fromMessage (Message <?> message , Class <?> targetClass ) {
167
+ return fromMessage (message , targetClass , null );
168
+ }
169
+
170
+ /**
171
+ * A variant of {@link #fromMessage(Message, Class)} which takes an extra
172
+ * conversion context as an argument, allowing to take e.g. annotations
173
+ * on a payload parameter into account.
174
+ * @param message the input message
175
+ * @param targetClass the target class for the conversion
176
+ * @param conversionHint an extra object passed to the {@link MessageConverter},
177
+ * e.g. the associated {@code MethodParameter} (may be {@code null}}
178
+ * @return the result of the conversion, or {@code null} if the converter cannot
179
+ * perform the conversion
180
+ * @since 4.2
181
+ * @see #fromMessage(Message, Class)
182
+ */
183
+ public final Object fromMessage (Message <?> message , Class <?> targetClass , Object conversionHint ) {
177
184
if (!canConvertFrom (message , targetClass )) {
178
185
return null ;
179
186
}
180
- return convertFromInternal (message , targetClass );
187
+ return convertFromInternal (message , targetClass , conversionHint );
181
188
}
182
189
183
190
protected boolean canConvertFrom (Message <?> message , Class <?> targetClass ) {
@@ -186,13 +193,33 @@ protected boolean canConvertFrom(Message<?> message, Class<?> targetClass) {
186
193
187
194
@ Override
188
195
public final Message <?> toMessage (Object payload , MessageHeaders headers ) {
196
+ return toMessage (payload , headers , null );
197
+ }
198
+
199
+ /**
200
+ * A variant of {@link #toMessage(Object, MessageHeaders)} which takes an extra
201
+ * conversion context as an argument, allowing to take e.g. annotations
202
+ * on a return type into account.
203
+ * @param payload the Object to convert
204
+ * @param headers optional headers for the message (may be {@code null})
205
+ * @param conversionHint an extra object passed to the {@link MessageConverter},
206
+ * e.g. the associated {@code MethodParameter} (may be {@code null}}
207
+ * @return the new message, or {@code null} if the converter does not support the
208
+ * Object type or the target media type
209
+ * @since 4.2
210
+ * @see #toMessage(Object, MessageHeaders)
211
+ */
212
+ public final Message <?> toMessage (Object payload , MessageHeaders headers , Object conversionHint ) {
189
213
if (!canConvertTo (payload , headers )) {
190
214
return null ;
191
215
}
192
216
193
- payload = convertToInternal (payload , headers );
194
- MimeType mimeType = getDefaultContentType (payload );
217
+ payload = convertToInternal (payload , headers , conversionHint );
218
+ if (payload == null ) {
219
+ return null ;
220
+ }
195
221
222
+ MimeType mimeType = getDefaultContentType (payload );
196
223
if (headers != null ) {
197
224
MessageHeaderAccessor accessor = MessageHeaderAccessor .getAccessor (headers , MessageHeaderAccessor .class );
198
225
if (accessor != null && accessor .isMutable ()) {
@@ -244,13 +271,52 @@ protected MimeType getMimeType(MessageHeaders headers) {
244
271
245
272
/**
246
273
* Convert the message payload from serialized form to an Object.
274
+ * @param message the input message
275
+ * @param targetClass the target class for the conversion
276
+ * @param conversionHint an extra object passed to the {@link MessageConverter},
277
+ * e.g. the associated {@code MethodParameter} (may be {@code null}}
278
+ * @return the result of the conversion, or {@code null} if the converter cannot
279
+ * perform the conversion
280
+ * @since 4.2
247
281
*/
248
- public abstract Object convertFromInternal (Message <?> message , Class <?> targetClass );
282
+ @ SuppressWarnings ("deprecation" )
283
+ protected Object convertFromInternal (Message <?> message , Class <?> targetClass , Object conversionHint ) {
284
+ return convertFromInternal (message , targetClass );
285
+ }
249
286
287
+ /**
288
+ * Convert the payload object to serialized form.
289
+ * @param payload the Object to convert
290
+ * @param headers optional headers for the message (may be {@code null})
291
+ * @param conversionHint an extra object passed to the {@link MessageConverter},
292
+ * e.g. the associated {@code MethodParameter} (may be {@code null}}
293
+ * @return the resulting payload for the message, or {@code null} if the converter
294
+ * cannot perform the conversion
295
+ * @since 4.2
296
+ */
297
+ @ SuppressWarnings ("deprecation" )
298
+ protected Object convertToInternal (Object payload , MessageHeaders headers , Object conversionHint ) {
299
+ return convertToInternal (payload , headers );
300
+ }
301
+
302
+ /**
303
+ * Convert the message payload from serialized form to an Object.
304
+ * @deprecated as of Spring 4.2, in favor of {@link #convertFromInternal(Message, Class, Object)}
305
+ * (which is also protected instead of public)
306
+ */
307
+ @ Deprecated
308
+ public Object convertFromInternal (Message <?> message , Class <?> targetClass ) {
309
+ return null ;
310
+ }
250
311
251
312
/**
252
313
* Convert the payload object to serialized form.
314
+ * @deprecated as of Spring 4.2, in favor of {@link #convertFromInternal(Message, Class, Object)}
315
+ * (which is also protected instead of public)
253
316
*/
254
- public abstract Object convertToInternal (Object payload , MessageHeaders headers );
317
+ @ Deprecated
318
+ public Object convertToInternal (Object payload , MessageHeaders headers ) {
319
+ return null ;
320
+ }
255
321
256
322
}
0 commit comments