Class JsonDeserializer<T>
- java.lang.Object
- 
- org.springframework.kafka.support.serializer.JsonDeserializer<T>
 
- 
- Type Parameters:
- T- class of the entity, representing messages
 - All Implemented Interfaces:
- java.io.Closeable,- java.lang.AutoCloseable,- org.apache.kafka.common.serialization.Deserializer<T>
 
 public class JsonDeserializer<T> extends java.lang.Object implements org.apache.kafka.common.serialization.Deserializer<T>GenericDeserializerfor receiving JSON from Kafka and return Java objects.- Author:
- Igor Stepanov, Artem Bilan, Gary Russell, Yanming Zhou, Elliot Kennedy, Torsten Schleede, Ivan Ponomarev
 
- 
- 
Field SummaryFields Modifier and Type Field Description static java.lang.StringKEY_DEFAULT_TYPEKafka config property for the default key type if no header.static java.lang.StringKEY_TYPE_METHODA method name to determine theJavaTypeto deserialize the key to: 'com.Foo.deserialize'.protected com.fasterxml.jackson.databind.ObjectMapperobjectMapperstatic java.lang.StringREMOVE_TYPE_INFO_HEADERSKafka config property for removing type headers (default true).protected com.fasterxml.jackson.databind.JavaTypetargetTypestatic java.lang.StringTRUSTED_PACKAGESKafka config property for trusted deserialization packages.static java.lang.StringTYPE_MAPPINGSKafka config property to add type mappings to the type mapper: 'foo=com.Foo,bar=com.Bar'.protected Jackson2JavaTypeMappertypeMapperstatic java.lang.StringUSE_TYPE_INFO_HEADERSKafka config property for using type headers (default true).static java.lang.StringVALUE_DEFAULT_TYPEKafka config property for the default value type if no header.static java.lang.StringVALUE_TYPE_METHODA method name to determine theJavaTypeto deserialize the value to: 'com.Foo.deserialize'.
 - 
Constructor SummaryConstructors Constructor Description JsonDeserializer()Construct an instance with a defaultObjectMapper.JsonDeserializer(com.fasterxml.jackson.core.type.TypeReference<? super T> targetType)Construct an instance with the provided target type, and a defaultObjectMapper.JsonDeserializer(com.fasterxml.jackson.core.type.TypeReference<? super T> targetType, boolean useHeadersIfPresent)Construct an instance with the provided target type, and useHeadersIfPresent with a defaultObjectMapper.JsonDeserializer(com.fasterxml.jackson.core.type.TypeReference<? super T> targetType, com.fasterxml.jackson.databind.ObjectMapper objectMapper)Construct an instance with the provided target type, andObjectMapper.JsonDeserializer(com.fasterxml.jackson.core.type.TypeReference<? super T> targetType, com.fasterxml.jackson.databind.ObjectMapper objectMapper, boolean useHeadersIfPresent)Construct an instance with the provided target type,ObjectMapperand useHeadersIfPresent.JsonDeserializer(com.fasterxml.jackson.databind.JavaType targetType)Construct an instance with the provided target type, and a defaultObjectMapper.JsonDeserializer(com.fasterxml.jackson.databind.JavaType targetType, boolean useHeadersIfPresent)Construct an instance with the provided target type, and useHeadersIfPresent with a defaultObjectMapper.JsonDeserializer(com.fasterxml.jackson.databind.JavaType targetType, com.fasterxml.jackson.databind.ObjectMapper objectMapper)Construct an instance with the provided target type, andObjectMapper.JsonDeserializer(com.fasterxml.jackson.databind.JavaType targetType, com.fasterxml.jackson.databind.ObjectMapper objectMapper, boolean useHeadersIfPresent)Construct an instance with the provided target type,ObjectMapperand useHeadersIfPresent.JsonDeserializer(com.fasterxml.jackson.databind.ObjectMapper objectMapper)Construct an instance with the providedObjectMapper.JsonDeserializer(java.lang.Class<? super T> targetType)Construct an instance with the provided target type, and a defaultObjectMapper.JsonDeserializer(java.lang.Class<? super T> targetType, boolean useHeadersIfPresent)Construct an instance with the provided target type, and useHeadersIfPresent with a defaultObjectMapper.JsonDeserializer(java.lang.Class<? super T> targetType, com.fasterxml.jackson.databind.ObjectMapper objectMapper)Construct an instance with the provided target type, andObjectMapper.JsonDeserializer(java.lang.Class<? super T> targetType, com.fasterxml.jackson.databind.ObjectMapper objectMapper, boolean useHeadersIfPresent)Construct an instance with the provided target type,ObjectMapperand useHeadersIfPresent.
 - 
Method SummaryAll Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddTrustedPackages(java.lang.String... packages)Add trusted packages for deserialization.voidclose()voidconfigure(java.util.Map<java.lang.String,?> configs, boolean isKey)<X> JsonDeserializer<X>copyWithType(com.fasterxml.jackson.core.type.TypeReference<? super X> newTargetType)Copies this deserializer with same configuration, except new target type reference is used.<X> JsonDeserializer<X>copyWithType(com.fasterxml.jackson.databind.JavaType newTargetType)Copies this deserializer with same configuration, except new target java type is used.<X> JsonDeserializer<X>copyWithType(java.lang.Class<? super X> newTargetType)Copies this deserializer with same configuration, except new target type is used.Tdeserialize(java.lang.String topic, byte[] data)Tdeserialize(java.lang.String topic, org.apache.kafka.common.header.Headers headers, byte[] data)JsonDeserializer<T>dontRemoveTypeHeaders()Don't remove type information headers.JsonDeserializer<T>forKeys()Designate this deserializer for deserializing keys (default is values); only applies if the default type mapper is used.Jackson2JavaTypeMappergetTypeMapper()JsonDeserializer<T>ignoreTypeHeaders()Ignore type information headers and use the configured target class.voidsetRemoveTypeHeaders(boolean removeTypeHeaders)Set to false to retain type information headers after deserialization.voidsetTypeFunction(java.util.function.BiFunction<byte[],org.apache.kafka.common.header.Headers,com.fasterxml.jackson.databind.JavaType> typeFunction)Set aBiFunctionthat receives the data to be deserialized and the headers and returns a JavaType.voidsetTypeMapper(Jackson2JavaTypeMapper typeMapper)Set a customized type mapper.voidsetTypeResolver(JsonTypeResolver typeResolver)Set aJsonTypeResolverthat receives the data to be deserialized and the headers and returns a JavaType.voidsetUseTypeHeaders(boolean useTypeHeaders)Set to false to ignore type information in headers and use the configured target type instead.voidsetUseTypeMapperForKey(boolean isKey)Configure the default Jackson2JavaTypeMapper to use key type headers.JsonDeserializer<T>trustedPackages(java.lang.String... packages)Add trusted packages to the default type mapper.JsonDeserializer<T>typeFunction(java.util.function.BiFunction<byte[],org.apache.kafka.common.header.Headers,com.fasterxml.jackson.databind.JavaType> typeFunction)Set aBiFunctionthat receives the data to be deserialized and the headers and returns a JavaType.JsonDeserializer<T>typeMapper(Jackson2JavaTypeMapper mapper)Use the suppliedJackson2JavaTypeMapper.JsonDeserializer<T>typeResolver(JsonTypeResolver resolver)Set aJsonTypeResolverthat receives the data to be deserialized and the headers and returns a JavaType.
 
- 
- 
- 
Field Detail- 
KEY_DEFAULT_TYPEpublic static final java.lang.String KEY_DEFAULT_TYPE Kafka config property for the default key type if no header.- See Also:
- Constant Field Values
 
 - 
VALUE_DEFAULT_TYPEpublic static final java.lang.String VALUE_DEFAULT_TYPE Kafka config property for the default value type if no header.- See Also:
- Constant Field Values
 
 - 
TRUSTED_PACKAGESpublic static final java.lang.String TRUSTED_PACKAGES Kafka config property for trusted deserialization packages.- See Also:
- Constant Field Values
 
 - 
TYPE_MAPPINGSpublic static final java.lang.String TYPE_MAPPINGS Kafka config property to add type mappings to the type mapper: 'foo=com.Foo,bar=com.Bar'.- See Also:
- Constant Field Values
 
 - 
REMOVE_TYPE_INFO_HEADERSpublic static final java.lang.String REMOVE_TYPE_INFO_HEADERS Kafka config property for removing type headers (default true).- See Also:
- Constant Field Values
 
 - 
USE_TYPE_INFO_HEADERSpublic static final java.lang.String USE_TYPE_INFO_HEADERS Kafka config property for using type headers (default true).- Since:
- 2.2.3
- See Also:
- Constant Field Values
 
 - 
KEY_TYPE_METHODpublic static final java.lang.String KEY_TYPE_METHOD A method name to determine theJavaTypeto deserialize the key to: 'com.Foo.deserialize'. SeeJsonTypeResolver.resolveType(java.lang.String, byte[], org.apache.kafka.common.header.Headers)for the signature.- See Also:
- Constant Field Values
 
 - 
VALUE_TYPE_METHODpublic static final java.lang.String VALUE_TYPE_METHOD A method name to determine theJavaTypeto deserialize the value to: 'com.Foo.deserialize'. SeeJsonTypeResolver.resolveType(java.lang.String, byte[], org.apache.kafka.common.header.Headers)for the signature.- See Also:
- Constant Field Values
 
 - 
objectMapperprotected final com.fasterxml.jackson.databind.ObjectMapper objectMapper 
 - 
targetTypeprotected com.fasterxml.jackson.databind.JavaType targetType 
 - 
typeMapperprotected Jackson2JavaTypeMapper typeMapper 
 
- 
 - 
Constructor Detail- 
JsonDeserializerpublic JsonDeserializer() Construct an instance with a defaultObjectMapper.
 - 
JsonDeserializerpublic JsonDeserializer(com.fasterxml.jackson.databind.ObjectMapper objectMapper) Construct an instance with the providedObjectMapper.- Parameters:
- objectMapper- a custom object mapper.
 
 - 
JsonDeserializerpublic JsonDeserializer(@Nullable java.lang.Class<? super T> targetType)Construct an instance with the provided target type, and a defaultObjectMapper.- Parameters:
- targetType- the target type to use if no type info headers are present.
 
 - 
JsonDeserializerpublic JsonDeserializer(@Nullable com.fasterxml.jackson.core.type.TypeReference<? super T> targetType)Construct an instance with the provided target type, and a defaultObjectMapper.- Parameters:
- targetType- the target type reference to use if no type info headers are present.
- Since:
- 2.3
 
 - 
JsonDeserializerpublic JsonDeserializer(@Nullable com.fasterxml.jackson.databind.JavaType targetType)Construct an instance with the provided target type, and a defaultObjectMapper.- Parameters:
- targetType- the target java type to use if no type info headers are present.
- Since:
- 2.3
 
 - 
JsonDeserializerpublic JsonDeserializer(@Nullable java.lang.Class<? super T> targetType, boolean useHeadersIfPresent)Construct an instance with the provided target type, and useHeadersIfPresent with a defaultObjectMapper.- Parameters:
- targetType- the target type.
- useHeadersIfPresent- true to use headers if present and fall back to target type if not.
- Since:
- 2.2
 
 - 
JsonDeserializerpublic JsonDeserializer(com.fasterxml.jackson.core.type.TypeReference<? super T> targetType, boolean useHeadersIfPresent) Construct an instance with the provided target type, and useHeadersIfPresent with a defaultObjectMapper.- Parameters:
- targetType- the target type reference.
- useHeadersIfPresent- true to use headers if present and fall back to target type if not.
- Since:
- 2.3
 
 - 
JsonDeserializerpublic JsonDeserializer(com.fasterxml.jackson.databind.JavaType targetType, boolean useHeadersIfPresent)Construct an instance with the provided target type, and useHeadersIfPresent with a defaultObjectMapper.- Parameters:
- targetType- the target java type.
- useHeadersIfPresent- true to use headers if present and fall back to target type if not.
- Since:
- 2.3
 
 - 
JsonDeserializerpublic JsonDeserializer(java.lang.Class<? super T> targetType, com.fasterxml.jackson.databind.ObjectMapper objectMapper) Construct an instance with the provided target type, andObjectMapper.- Parameters:
- targetType- the target type to use if no type info headers are present.
- objectMapper- the mapper. type if not.
 
 - 
JsonDeserializerpublic JsonDeserializer(com.fasterxml.jackson.core.type.TypeReference<? super T> targetType, com.fasterxml.jackson.databind.ObjectMapper objectMapper) Construct an instance with the provided target type, andObjectMapper.- Parameters:
- targetType- the target type reference to use if no type info headers are present.
- objectMapper- the mapper. type if not.
 
 - 
JsonDeserializerpublic JsonDeserializer(com.fasterxml.jackson.databind.JavaType targetType, com.fasterxml.jackson.databind.ObjectMapper objectMapper)Construct an instance with the provided target type, andObjectMapper.- Parameters:
- targetType- the target java type to use if no type info headers are present.
- objectMapper- the mapper. type if not.
 
 - 
JsonDeserializerpublic JsonDeserializer(@Nullable java.lang.Class<? super T> targetType, com.fasterxml.jackson.databind.ObjectMapper objectMapper, boolean useHeadersIfPresent)Construct an instance with the provided target type,ObjectMapperand useHeadersIfPresent.- Parameters:
- targetType- the target type.
- objectMapper- the mapper.
- useHeadersIfPresent- true to use headers if present and fall back to target type if not.
- Since:
- 2.2
 
 - 
JsonDeserializerpublic JsonDeserializer(com.fasterxml.jackson.core.type.TypeReference<? super T> targetType, com.fasterxml.jackson.databind.ObjectMapper objectMapper, boolean useHeadersIfPresent) Construct an instance with the provided target type,ObjectMapperand useHeadersIfPresent.- Parameters:
- targetType- the target type reference.
- objectMapper- the mapper.
- useHeadersIfPresent- true to use headers if present and fall back to target type if not.
- Since:
- 2.3
 
 - 
JsonDeserializerpublic JsonDeserializer(@Nullable com.fasterxml.jackson.databind.JavaType targetType, com.fasterxml.jackson.databind.ObjectMapper objectMapper, boolean useHeadersIfPresent)Construct an instance with the provided target type,ObjectMapperand useHeadersIfPresent.- Parameters:
- targetType- the target type reference.
- objectMapper- the mapper.
- useHeadersIfPresent- true to use headers if present and fall back to target type if not.
- Since:
- 2.3
 
 
- 
 - 
Method Detail- 
getTypeMapperpublic Jackson2JavaTypeMapper getTypeMapper() 
 - 
setTypeMapperpublic void setTypeMapper(Jackson2JavaTypeMapper typeMapper) Set a customized type mapper. If the mapper is anAbstractJavaTypeMapper, any class mappings configured in the mapper will be added to the trusted packages.- Parameters:
- typeMapper- the type mapper.
- Since:
- 2.1
 
 - 
setUseTypeMapperForKeypublic void setUseTypeMapperForKey(boolean isKey) Configure the default Jackson2JavaTypeMapper to use key type headers.- Parameters:
- isKey- Use key type headers if true
- Since:
- 2.1.3
 
 - 
setRemoveTypeHeaderspublic void setRemoveTypeHeaders(boolean removeTypeHeaders) Set to false to retain type information headers after deserialization. Default true.- Parameters:
- removeTypeHeaders- true to remove headers.
- Since:
- 2.2
 
 - 
setUseTypeHeaderspublic void setUseTypeHeaders(boolean useTypeHeaders) Set to false to ignore type information in headers and use the configured target type instead. Only applies if the preconfigured type mapper is used. Default true.- Parameters:
- useTypeHeaders- false to ignore type headers.
- Since:
- 2.2.8
 
 - 
setTypeFunctionpublic void setTypeFunction(java.util.function.BiFunction<byte[],org.apache.kafka.common.header.Headers,com.fasterxml.jackson.databind.JavaType> typeFunction) Set aBiFunctionthat receives the data to be deserialized and the headers and returns a JavaType.- Parameters:
- typeFunction- the function.
- Since:
- 2.5
 
 - 
setTypeResolverpublic void setTypeResolver(JsonTypeResolver typeResolver) Set aJsonTypeResolverthat receives the data to be deserialized and the headers and returns a JavaType.- Parameters:
- typeResolver- the resolver.
- Since:
- 2.5.3
 
 - 
configurepublic void configure(java.util.Map<java.lang.String,?> configs, boolean isKey)- Specified by:
- configurein interface- org.apache.kafka.common.serialization.Deserializer<T>
 
 - 
addTrustedPackagespublic void addTrustedPackages(java.lang.String... packages) Add trusted packages for deserialization.- Parameters:
- packages- the packages.
- Since:
- 2.1
 
 - 
deserializepublic T deserialize(java.lang.String topic, org.apache.kafka.common.header.Headers headers, byte[] data) - Specified by:
- deserializein interface- org.apache.kafka.common.serialization.Deserializer<T>
 
 - 
deserializepublic T deserialize(java.lang.String topic, @Nullable byte[] data) - Specified by:
- deserializein interface- org.apache.kafka.common.serialization.Deserializer<T>
 
 - 
closepublic void close() - Specified by:
- closein interface- java.lang.AutoCloseable
- Specified by:
- closein interface- java.io.Closeable
- Specified by:
- closein interface- org.apache.kafka.common.serialization.Deserializer<T>
 
 - 
copyWithTypepublic <X> JsonDeserializer<X> copyWithType(java.lang.Class<? super X> newTargetType) Copies this deserializer with same configuration, except new target type is used.- Type Parameters:
- X- new deserialization result type
- Parameters:
- newTargetType- type used for when type headers are missing, not null
- Returns:
- new instance of deserializer with type changes
- Since:
- 2.6
 
 - 
copyWithTypepublic <X> JsonDeserializer<X> copyWithType(com.fasterxml.jackson.core.type.TypeReference<? super X> newTargetType) Copies this deserializer with same configuration, except new target type reference is used.- Type Parameters:
- X- new deserialization result type
- Parameters:
- newTargetType- type reference used for when type headers are missing, not null
- Returns:
- new instance of deserializer with type changes
- Since:
- 2.6
 
 - 
copyWithTypepublic <X> JsonDeserializer<X> copyWithType(com.fasterxml.jackson.databind.JavaType newTargetType) Copies this deserializer with same configuration, except new target java type is used.- Type Parameters:
- X- new deserialization result type
- Parameters:
- newTargetType- java type used for when type headers are missing, not null
- Returns:
- new instance of deserializer with type changes
- Since:
- 2.6
 
 - 
forKeyspublic JsonDeserializer<T> forKeys() Designate this deserializer for deserializing keys (default is values); only applies if the default type mapper is used.- Returns:
- the deserializer.
- Since:
- 2.3
 
 - 
dontRemoveTypeHeaderspublic JsonDeserializer<T> dontRemoveTypeHeaders() Don't remove type information headers.- Returns:
- the deserializer.
- Since:
- 2.3
- See Also:
- setRemoveTypeHeaders(boolean)
 
 - 
ignoreTypeHeaderspublic JsonDeserializer<T> ignoreTypeHeaders() Ignore type information headers and use the configured target class.- Returns:
- the deserializer.
- Since:
- 2.3
- See Also:
- setUseTypeHeaders(boolean)
 
 - 
typeMapperpublic JsonDeserializer<T> typeMapper(Jackson2JavaTypeMapper mapper) Use the suppliedJackson2JavaTypeMapper.- Parameters:
- mapper- the mapper.
- Returns:
- the deserializer.
- Since:
- 2.3
- See Also:
- setTypeMapper(Jackson2JavaTypeMapper)
 
 - 
trustedPackagespublic JsonDeserializer<T> trustedPackages(java.lang.String... packages) Add trusted packages to the default type mapper.- Parameters:
- packages- the packages.
- Returns:
- the deserializer.
- Since:
- 2,5
 
 - 
typeFunctionpublic JsonDeserializer<T> typeFunction(java.util.function.BiFunction<byte[],org.apache.kafka.common.header.Headers,com.fasterxml.jackson.databind.JavaType> typeFunction) Set aBiFunctionthat receives the data to be deserialized and the headers and returns a JavaType.- Parameters:
- typeFunction- the function.
- Returns:
- the deserializer.
- Since:
- 2.5
 
 - 
typeResolverpublic JsonDeserializer<T> typeResolver(JsonTypeResolver resolver) Set aJsonTypeResolverthat receives the data to be deserialized and the headers and returns a JavaType.- Parameters:
- resolver- the resolver.
- Returns:
- the deserializer.
- Since:
- 2.5.3
 
 
- 
 
-