Class AbstractJacksonMessageConverter
java.lang.Object
org.springframework.amqp.support.converter.AbstractMessageConverter
org.springframework.amqp.support.converter.AbstractJacksonMessageConverter
- All Implemented Interfaces:
MessageConverter,SmartMessageConverter,Aware,BeanClassLoaderAware
- Direct Known Subclasses:
JacksonJsonMessageConverter,JacksonXmlMessageConverter
public abstract class AbstractJacksonMessageConverter
extends AbstractMessageConverter
implements BeanClassLoaderAware, SmartMessageConverter
Abstract Jackson 3 message converter.
- Since:
- 4.0
- Author:
- Artem Bilan
-
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractJacksonMessageConverter(tools.jackson.databind.ObjectMapper objectMapper, MimeType contentType, String... trustedPackages) Construct with the providedObjectMapperinstance. -
Method Summary
Modifier and TypeMethodDescriptionprotected ObjectconvertContent(Message message, @Nullable Object conversionHint, MessageProperties properties, @Nullable String encoding) protected MessagecreateMessage(Object objectToConvert, MessageProperties messageProperties) Crate a message from the payload object and message properties provided.protected MessagecreateMessage(Object objectToConvert, MessageProperties messageProperties, @Nullable Type genericType) Crate a message from the payload object and message properties provided.protected ObjectdoFromMessage(Message message, @Nullable Object conversionHint, MessageProperties properties, @Nullable String encoding) fromMessage(Message message) Convert from a Message to a Java object.fromMessage(Message message, @Nullable Object conversionHint) A variant ofMessageConverter.fromMessage(Message)which takes an extra conversion context as an argument.protected @Nullable ClassLoader@Nullable ClassMapperprotected MimeTypeGet the supported content type; only the subtype is checked when decoding, e.g.Return the type precedence.booleanWhether an explicit java type mapper has been provided.voidsetAlwaysConvertToInferredType(boolean alwaysAttemptConversion) When false (default), fall back to type id headers if the type (or contents of a container type) is abstract.voidsetAssumeSupportedContentType(boolean assumeSupportedContentType) By default, the supported content type is assumed when there is no contentType property, or it is set to the default ('application/octet-stream').voidsetBeanClassLoader(ClassLoader classLoader) voidsetClassMapper(ClassMapper classMapper) voidsetDefaultCharset(@Nullable String defaultCharset) Specify the default charset to use when converting to or from text-based Message body content.voidsetJavaTypeMapper(JacksonJavaTypeMapper javaTypeMapper) voidsetNullAsOptionalEmpty(boolean nullAsOptionalEmpty) When true, if jackson decodes the body asnullconvert toOptional.empty()instead of returning the original body.voidsetSupportedContentType(MimeType supportedContentType) Set the supported content type; only the subtype is checked when decoding, e.g.voidsetTypePrecedence(JacksonJavaTypeMapper.TypePrecedence typePrecedence) Set the precedence for evaluating type information in message properties.Methods inherited from class org.springframework.amqp.support.converter.AbstractMessageConverter
isCreateMessageIds, setCreateMessageIds, toMessage, toMessageMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.amqp.support.converter.MessageConverter
toMessage, toMessage
-
Field Details
-
DEFAULT_CHARSET
-
log
protected final org.apache.commons.logging.Log log -
objectMapper
protected final tools.jackson.databind.ObjectMapper objectMapper
-
-
Constructor Details
-
AbstractJacksonMessageConverter
protected AbstractJacksonMessageConverter(tools.jackson.databind.ObjectMapper objectMapper, MimeType contentType, String... trustedPackages) Construct with the providedObjectMapperinstance.- Parameters:
objectMapper- theObjectMapperto use.contentType- the supported content type; only the subtype is checked when decoding, e.g. */json, */xml. If this contains a charset parameter, when encoding, the contentType header will not be set, when decoding, the raw bytes are passed to Jackson which can dynamically determine the encoding; otherwise the contentEncoding or default charset is used.trustedPackages- the trusted Java packages for deserialization- See Also:
-
-
Method Details
-
getSupportedContentType
Get the supported content type; only the subtype is checked when decoding, e.g. */json, */xml. If this contains a charset parameter, when encoding, the contentType header will not be set, when decoding, the raw bytes are passed to Jackson which can dynamically determine the encoding; otherwise the contentEncoding or default charset is used.- Returns:
- the supportedContentType
-
setSupportedContentType
Set the supported content type; only the subtype is checked when decoding, e.g. */json, */xml. If this contains a charset parameter, when encoding, the contentType header will not be set, when decoding, the raw bytes are passed to Jackson which can dynamically determine the encoding; otherwise the contentEncoding or default charset is used.- Parameters:
supportedContentType- the supportedContentType to set.
-
setNullAsOptionalEmpty
public void setNullAsOptionalEmpty(boolean nullAsOptionalEmpty) When true, if jackson decodes the body asnullconvert toOptional.empty()instead of returning the original body. Default false.- Parameters:
nullAsOptionalEmpty- true to return empty.
-
getClassMapper
-
setClassMapper
-
setDefaultCharset
Specify the default charset to use when converting to or from text-based Message body content. If not specified, the charset will be "UTF-8".- Parameters:
defaultCharset- The default charset.
-
getDefaultCharset
-
setBeanClassLoader
- Specified by:
setBeanClassLoaderin interfaceBeanClassLoaderAware
-
getClassLoader
-
getJavaTypeMapper
-
isTypeMapperSet
public boolean isTypeMapperSet()Whether an explicit java type mapper has been provided.- Returns:
- false if the default type mapper is being used.
- See Also:
-
setJavaTypeMapper
-
getTypePrecedence
Return the type precedence.- Returns:
- the precedence.
- See Also:
-
setTypePrecedence
Set the precedence for evaluating type information in message properties. When using@RabbitListenerat the method level, the framework attempts to determine the target type for payload conversion from the method signature. If so, this type is provided in theinferredArgumentTypemessage property.By default, if the type is concrete (not abstract, not an interface), this will be used ahead of type information provided in the
__TypeId__and associated headers provided by the sender.If you wish to force the use of the
__TypeId__and associated headers (such as when the actual type is a subclass of the method argument type), set the precedence toJacksonJavaTypeMapper.TypePrecedence.TYPE_ID.- Parameters:
typePrecedence- the precedence.- See Also:
-
setAlwaysConvertToInferredType
public void setAlwaysConvertToInferredType(boolean alwaysAttemptConversion) When false (default), fall back to type id headers if the type (or contents of a container type) is abstract. Set to true if conversion should always be attempted - perhaps because a custom deserializer has been configured on theObjectMapper. If the attempt fails, fall back to headers.- Parameters:
alwaysAttemptConversion- true to attempt.
-
setAssumeSupportedContentType
public void setAssumeSupportedContentType(boolean assumeSupportedContentType) By default, the supported content type is assumed when there is no contentType property, or it is set to the default ('application/octet-stream'). Set to 'false' to revert to the previous behavior of returning an unconverted 'byte[]' when this condition exists.- Parameters:
assumeSupportedContentType- set false to not assume the content type is supported.
-
fromMessage
Description copied from interface:MessageConverterConvert from a Message to a Java object.- Specified by:
fromMessagein interfaceMessageConverter- Parameters:
message- the message to convert- Returns:
- the converted Java object
- Throws:
MessageConversionException- in case of conversion failure
-
fromMessage
public Object fromMessage(Message message, @Nullable Object conversionHint) throws MessageConversionException A variant ofMessageConverter.fromMessage(Message)which takes an extra conversion context as an argument.- Specified by:
fromMessagein interfaceSmartMessageConverter- Parameters:
message- the input message.conversionHint- The conversionHint must be aParameterizedTypeReference.- Returns:
- the result of the conversion, or
nullif the converter cannot perform the conversion. - Throws:
MessageConversionException- if the conversion fails.- See Also:
-
doFromMessage
protected Object doFromMessage(Message message, @Nullable Object conversionHint, MessageProperties properties, @Nullable String encoding) -
convertContent
protected Object convertContent(Message message, @Nullable Object conversionHint, MessageProperties properties, @Nullable String encoding) throws IOException - Throws:
IOException
-
createMessage
protected Message createMessage(Object objectToConvert, MessageProperties messageProperties) throws MessageConversionException Description copied from class:AbstractMessageConverterCrate a message from the payload object and message properties provided. The message id will be added to the properties if necessary later.- Specified by:
createMessagein classAbstractMessageConverter- Parameters:
objectToConvert- the payload.messageProperties- the message properties (headers).- Returns:
- a message.
- Throws:
MessageConversionException
-
createMessage
protected Message createMessage(Object objectToConvert, MessageProperties messageProperties, @Nullable Type genericType) throws MessageConversionException Description copied from class:AbstractMessageConverterCrate a message from the payload object and message properties provided. The message id will be added to the properties if necessary later.- Overrides:
createMessagein classAbstractMessageConverter- Parameters:
objectToConvert- the payloadmessageProperties- the message properties (headers)genericType- the type to convert from - used to populate type headers.- Returns:
- a message
- Throws:
MessageConversionException
-