Class AbstractMessageConverter
- All Implemented Interfaces:
- MessageConverter,- SmartMessageConverter
- Direct Known Subclasses:
- AbstractJsonMessageConverter,- ByteArrayMessageConverter,- JacksonJsonMessageConverter,- MappingJackson2MessageConverter,- MarshallingMessageConverter,- ProtobufMessageConverter,- StringMessageConverter
SmartMessageConverter implementations including
 support for common properties and a partial implementation of the conversion methods,
 mainly to check if the converter supports the conversion based on the payload class
 and MIME type.- Since:
- 4.0
- Author:
- Rossen Stoyanchev, Sebastien Deleuze, Juergen Hoeller
- 
Field SummaryFields
- 
Constructor SummaryConstructorsModifierConstructorDescriptionprotectedAbstractMessageConverter(Collection<MimeType> supportedMimeTypes) Constructor with Collection of MIME types.protectedAbstractMessageConverter(MimeType supportedMimeType) Constructor with a single MIME type.protectedAbstractMessageConverter(MimeType... supportedMimeTypes) Constructor with multiple MIME types.
- 
Method SummaryModifier and TypeMethodDescriptionprotected voidaddSupportedMimeTypes(MimeType... supportedMimeTypes) Allows subclasses to add more supported mime types.protected booleancanConvertFrom(Message<?> message, Class<?> targetClass) protected booleancanConvertTo(Object payload, @Nullable MessageHeaders headers) convertFromInternal(Message<?> message, Class<?> targetClass, @Nullable Object conversionHint) Convert the message payload from serialized form to an Object.convertToInternal(Object payload, @Nullable MessageHeaders headers, @Nullable Object conversionHint) Convert the payload object to serialized form.fromMessage(Message<?> message, Class<?> targetClass) Convert the payload of aMessagefrom a serialized form to a typed Object of the specified target class.fromMessage(Message<?> message, Class<?> targetClass, @Nullable Object conversionHint) A variant ofMessageConverter.fromMessage(Message, Class)which takes an extra conversion context as an argument, allowing to take, for example, annotations on a payload parameter into account.Return theconfiguredContentTypeResolver.getDefaultContentType(Object payload) Return the default content type for the payload.getMimeType(@Nullable MessageHeaders headers) Class<?> Return the configured preferred serialization payload class.Return the supported MIME types.booleanWhether content type resolution must produce a value that matches one of the supported MIME types.voidConfigure aContentTypeResolverfor resolving the content type of input messages.voidsetSerializedPayloadClass(Class<?> payloadClass) Configure the preferred serialization class to use (byte[] or String) when converting an Object payload to aMessage.voidsetStrictContentTypeMatch(boolean strictContentTypeMatch) Whether this converter should convert messages for which no content type can be resolved through the configuredContentTypeResolver.protected abstract booleanWhether the given class is supported by this converter.protected booleansupportsMimeType(@Nullable MessageHeaders headers) toMessage(Object payload, @Nullable MessageHeaders headers) Create aMessagewhose payload is the result of converting the given payload Object to serialized form.A variant ofMessageConverter.toMessage(Object, MessageHeaders)which takes an extra conversion context as an argument, allowing to take, for example, annotations on a return type into account.
- 
Field Details- 
loggerprotected final org.apache.commons.logging.Log logger
 
- 
- 
Constructor Details- 
AbstractMessageConverterConstructor with a single MIME type.
- 
AbstractMessageConverterConstructor with multiple MIME types.- Since:
- 5.2.2
 
- 
AbstractMessageConverterConstructor with Collection of MIME types.
 
- 
- 
Method Details- 
getSupportedMimeTypes
- 
addSupportedMimeTypesAllows subclasses to add more supported mime types.- Since:
- 5.2.2
 
- 
setContentTypeResolverConfigure aContentTypeResolverfor resolving the content type of input messages.By default, a DefaultContentTypeResolverinstance is used.Note: if the resolver is set to null, thenstrictContentTypeMatchshould befalse, which is the default, or otherwise this converter will ignore all messages.
- 
getContentTypeResolverReturn theconfiguredContentTypeResolver.
- 
setStrictContentTypeMatchpublic void setStrictContentTypeMatch(boolean strictContentTypeMatch) Whether this converter should convert messages for which no content type can be resolved through the configuredContentTypeResolver.A converter can be configured to be strict only when a contentTypeResolveris configured and the list ofsupportedMimeTypesis not empty.When this flag is set to true,supportsMimeType(MessageHeaders)will returnfalseif thecontentTypeResolveris not defined or if no content-type header is present.
- 
isStrictContentTypeMatchpublic boolean isStrictContentTypeMatch()Whether content type resolution must produce a value that matches one of the supported MIME types.
- 
setSerializedPayloadClass
- 
getSerializedPayloadClassReturn the configured preferred serialization payload class.
- 
fromMessageDescription copied from interface:MessageConverterConvert the payload of aMessagefrom a serialized form to a typed Object of the specified target class. TheMessageHeaders.CONTENT_TYPEheader should indicate the MIME type to convert from.If the converter does not support the specified media type or cannot perform the conversion, it should return null.- Specified by:
- fromMessagein interface- MessageConverter
- Parameters:
- message- the input message
- targetClass- the target class for the conversion
- Returns:
- the result of the conversion, or nullif the converter cannot perform the conversion
 
- 
fromMessagepublic final @Nullable Object fromMessage(Message<?> message, Class<?> targetClass, @Nullable Object conversionHint) Description copied from interface:SmartMessageConverterA variant ofMessageConverter.fromMessage(Message, Class)which takes an extra conversion context as an argument, allowing to take, for example, annotations on a payload parameter into account.- Specified by:
- fromMessagein interface- SmartMessageConverter
- Parameters:
- message- the input message
- targetClass- the target class for the conversion
- conversionHint- an extra object passed to the- MessageConverter, for example, the associated- MethodParameter(may be- null)
- Returns:
- the result of the conversion, or nullif the converter cannot perform the conversion
- See Also:
 
- 
toMessageDescription copied from interface:MessageConverterCreate aMessagewhose payload is the result of converting the given payload Object to serialized form. The optionalMessageHeadersparameter may contain aMessageHeaders.CONTENT_TYPEheader to specify the target media type for the conversion and it may contain additional headers to be added to the message.If the converter does not support the specified media type or cannot perform the conversion, it should return null.- Specified by:
- toMessagein interface- MessageConverter
- Parameters:
- payload- the Object to convert
- headers- optional headers for the message (may be- null)
- Returns:
- the new message, or nullif the converter does not support the Object type or the target media type
 
- 
toMessagepublic final @Nullable Message<?> toMessage(Object payload, @Nullable MessageHeaders headers, @Nullable Object conversionHint) Description copied from interface:SmartMessageConverterA variant ofMessageConverter.toMessage(Object, MessageHeaders)which takes an extra conversion context as an argument, allowing to take, for example, annotations on a return type into account.- Specified by:
- toMessagein interface- SmartMessageConverter
- Parameters:
- payload- the Object to convert
- headers- optional headers for the message (may be- null)
- conversionHint- an extra object passed to the- MessageConverter, for example, the associated- MethodParameter(may be- null)
- Returns:
- the new message, or nullif the converter does not support the Object type or the target media type
- See Also:
 
- 
canConvertFrom
- 
canConvertTo
- 
supportsMimeType
- 
getMimeType
- 
getDefaultContentTypeReturn the default content type for the payload. Called whentoMessage(Object, MessageHeaders)is invoked without message headers or without a content type header.By default, this returns the first element of the supportedMimeTypes, if any. Can be overridden in subclasses.- Parameters:
- payload- the payload being converted to a message
- Returns:
- the content type, or nullif not known
 
- 
supportsWhether the given class is supported by this converter.- Parameters:
- clazz- the class to test for support
- Returns:
- trueif supported;- falseotherwise
 
- 
convertFromInternalprotected @Nullable Object convertFromInternal(Message<?> message, Class<?> targetClass, @Nullable Object conversionHint) Convert the message payload from serialized form to an Object.- Parameters:
- message- the input message
- targetClass- the target class for the conversion
- conversionHint- an extra object passed to the- MessageConverter, for example, the associated- MethodParameter(may be- null)
- Returns:
- the result of the conversion, or nullif the converter cannot perform the conversion
- Since:
- 4.2
 
- 
convertToInternalprotected @Nullable Object convertToInternal(Object payload, @Nullable MessageHeaders headers, @Nullable Object conversionHint) Convert the payload object to serialized form.- Parameters:
- payload- the Object to convert
- headers- optional headers for the message (may be- null)
- conversionHint- an extra object passed to the- MessageConverter, for example, the associated- MethodParameter(may be- null)
- Returns:
- the resulting payload for the message, or nullif the converter cannot perform the conversion
- Since:
- 4.2
 
 
-