T - class of the entity, representing messagespublic class JsonDeserializer<T>
extends java.lang.Object
implements org.apache.kafka.common.serialization.Deserializer<T>
Deserializer for
receiving JSON from Kafka and return Java objects.| Modifier and Type | Field and Description |
|---|---|
static java.lang.String |
DEFAULT_KEY_TYPE
Deprecated.
in favor of
KEY_DEFAULT_TYPE |
static java.lang.String |
DEFAULT_VALUE_TYPE
Deprecated.
in favor of
VALUE_DEFAULT_TYPE |
static java.lang.String |
KEY_DEFAULT_TYPE
Kafka config property for the default key type if no header.
|
protected com.fasterxml.jackson.databind.ObjectMapper |
objectMapper |
static java.lang.String |
REMOVE_TYPE_INFO_HEADERS
Kafka config property for removing type headers (default true).
|
protected com.fasterxml.jackson.databind.JavaType |
targetType |
static java.lang.String |
TRUSTED_PACKAGES
Kafka config property for trusted deserialization packages.
|
static java.lang.String |
TYPE_MAPPINGS
Kafka config property to add type mappings to the type mapper:
'foo=com.Foo,bar=com.Bar'.
|
protected Jackson2JavaTypeMapper |
typeMapper |
static java.lang.String |
USE_TYPE_INFO_HEADERS
Kafka config property for using type headers (default true).
|
static java.lang.String |
VALUE_DEFAULT_TYPE
Kafka config property for the default value type if no header.
|
| Constructor and Description |
|---|
JsonDeserializer()
Construct an instance with a default
ObjectMapper. |
JsonDeserializer(java.lang.Class<? super T> targetType)
Construct an instance with the provided target type, and a default
ObjectMapper. |
JsonDeserializer(java.lang.Class<? super T> targetType,
boolean useHeadersIfPresent)
Construct an instance with the provided target type, and
useHeadersIfPresent with a default
ObjectMapper. |
JsonDeserializer(java.lang.Class<? super T> targetType,
com.fasterxml.jackson.databind.ObjectMapper objectMapper)
Construct an instance with the provided target type, and
ObjectMapper. |
JsonDeserializer(java.lang.Class<? super T> targetType,
com.fasterxml.jackson.databind.ObjectMapper objectMapper,
boolean useHeadersIfPresent)
Construct an instance with the provided target type,
ObjectMapper and
useHeadersIfPresent. |
JsonDeserializer(com.fasterxml.jackson.databind.JavaType targetType,
com.fasterxml.jackson.databind.ObjectMapper objectMapper,
boolean useHeadersIfPresent)
Construct an instance with the provided target type,
ObjectMapper and
useHeadersIfPresent. |
JsonDeserializer(com.fasterxml.jackson.databind.ObjectMapper objectMapper)
Construct an instance with the provided
ObjectMapper. |
JsonDeserializer(com.fasterxml.jackson.core.type.TypeReference<? super T> targetType)
Construct an instance with the provided target type, and a default
ObjectMapper. |
JsonDeserializer(com.fasterxml.jackson.core.type.TypeReference<? super T> targetType,
boolean useHeadersIfPresent)
Construct an instance with the provided target type, and
useHeadersIfPresent with a default
ObjectMapper. |
JsonDeserializer(com.fasterxml.jackson.core.type.TypeReference<? super T> targetType,
com.fasterxml.jackson.databind.ObjectMapper objectMapper)
Construct an instance with the provided target type, and
ObjectMapper. |
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,
ObjectMapper and
useHeadersIfPresent. |
| Modifier and Type | Method and Description |
|---|---|
void |
addTrustedPackages(java.lang.String... packages)
Add trusted packages for deserialization.
|
void |
close() |
void |
configure(java.util.Map<java.lang.String,?> configs,
boolean isKey) |
T |
deserialize(java.lang.String topic,
byte[] data) |
T |
deserialize(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.
|
Jackson2JavaTypeMapper |
getTypeMapper() |
JsonDeserializer<T> |
ignoreTypeHeaders()
Ignore type information headers and use the configured target class.
|
void |
setRemoveTypeHeaders(boolean removeTypeHeaders)
Set to false to retain type information headers after deserialization.
|
void |
setTypeMapper(Jackson2JavaTypeMapper typeMapper)
Set a customized type mapper.
|
void |
setUseTypeHeaders(boolean useTypeHeaders)
Set to false to ignore type information in headers and use the configured
target type instead.
|
void |
setUseTypeMapperForKey(boolean isKey)
Configure the default Jackson2JavaTypeMapper to use key type headers.
|
JsonDeserializer<T> |
typeMapper(Jackson2JavaTypeMapper mapper)
Use the supplied
Jackson2JavaTypeMapper. |
@Deprecated public static final java.lang.String DEFAULT_KEY_TYPE
KEY_DEFAULT_TYPE@Deprecated public static final java.lang.String DEFAULT_VALUE_TYPE
VALUE_DEFAULT_TYPEpublic static final java.lang.String KEY_DEFAULT_TYPE
public static final java.lang.String VALUE_DEFAULT_TYPE
public static final java.lang.String TRUSTED_PACKAGES
public static final java.lang.String TYPE_MAPPINGS
public static final java.lang.String REMOVE_TYPE_INFO_HEADERS
public static final java.lang.String USE_TYPE_INFO_HEADERS
protected final com.fasterxml.jackson.databind.ObjectMapper objectMapper
protected com.fasterxml.jackson.databind.JavaType targetType
protected Jackson2JavaTypeMapper typeMapper
public JsonDeserializer()
ObjectMapper.public JsonDeserializer(com.fasterxml.jackson.databind.ObjectMapper objectMapper)
ObjectMapper.objectMapper - a custom object mapper.public JsonDeserializer(java.lang.Class<? super T> targetType)
ObjectMapper.targetType - the target type to use if no type info headers are present.public JsonDeserializer(com.fasterxml.jackson.core.type.TypeReference<? super T> targetType)
ObjectMapper.targetType - the target type reference to use if no type info headers are present.public JsonDeserializer(@Nullable
java.lang.Class<? super T> targetType,
boolean useHeadersIfPresent)
ObjectMapper.targetType - the target type.useHeadersIfPresent - true to use headers if present and fall back to target
type if not.public JsonDeserializer(com.fasterxml.jackson.core.type.TypeReference<? super T> targetType, boolean useHeadersIfPresent)
ObjectMapper.targetType - the target type reference.useHeadersIfPresent - true to use headers if present and fall back to target
type if not.public JsonDeserializer(java.lang.Class<? super T> targetType, com.fasterxml.jackson.databind.ObjectMapper objectMapper)
ObjectMapper.targetType - the target type to use if no type info headers are present.objectMapper - the mapper. type if not.public JsonDeserializer(com.fasterxml.jackson.core.type.TypeReference<? super T> targetType, com.fasterxml.jackson.databind.ObjectMapper objectMapper)
ObjectMapper.targetType - the target type reference to use if no type info headers are present.objectMapper - the mapper. type if not.public JsonDeserializer(@Nullable
java.lang.Class<? super T> targetType,
com.fasterxml.jackson.databind.ObjectMapper objectMapper,
boolean useHeadersIfPresent)
ObjectMapper and
useHeadersIfPresent.targetType - the target type.objectMapper - the mapper.useHeadersIfPresent - true to use headers if present and fall back to target
type if not.public JsonDeserializer(com.fasterxml.jackson.core.type.TypeReference<? super T> targetType, com.fasterxml.jackson.databind.ObjectMapper objectMapper, boolean useHeadersIfPresent)
ObjectMapper and
useHeadersIfPresent.targetType - the target type reference.objectMapper - the mapper.useHeadersIfPresent - true to use headers if present and fall back to target
type if not.public JsonDeserializer(@Nullable
com.fasterxml.jackson.databind.JavaType targetType,
com.fasterxml.jackson.databind.ObjectMapper objectMapper,
boolean useHeadersIfPresent)
ObjectMapper and
useHeadersIfPresent.targetType - the target type reference.objectMapper - the mapper.useHeadersIfPresent - true to use headers if present and fall back to target
type if not.public Jackson2JavaTypeMapper getTypeMapper()
public void setTypeMapper(Jackson2JavaTypeMapper typeMapper)
typeMapper - the type mapper.public void setUseTypeMapperForKey(boolean isKey)
isKey - Use key type headers if truepublic void setRemoveTypeHeaders(boolean removeTypeHeaders)
removeTypeHeaders - true to remove headers.public void setUseTypeHeaders(boolean useTypeHeaders)
useTypeHeaders - false to ignore type headers.public void configure(java.util.Map<java.lang.String,?> configs,
boolean isKey)
configure in interface org.apache.kafka.common.serialization.Deserializer<T>public void addTrustedPackages(java.lang.String... packages)
packages - the packages.public T deserialize(java.lang.String topic, org.apache.kafka.common.header.Headers headers, byte[] data)
deserialize in interface org.apache.kafka.common.serialization.Deserializer<T>public T deserialize(java.lang.String topic, @Nullable byte[] data)
deserialize in interface org.apache.kafka.common.serialization.Deserializer<T>public void close()
close in interface java.io.Closeableclose in interface java.lang.AutoCloseableclose in interface org.apache.kafka.common.serialization.Deserializer<T>public JsonDeserializer<T> forKeys()
public JsonDeserializer<T> dontRemoveTypeHeaders()
setRemoveTypeHeaders(boolean)public JsonDeserializer<T> ignoreTypeHeaders()
setUseTypeHeaders(boolean)public JsonDeserializer<T> typeMapper(Jackson2JavaTypeMapper mapper)
Jackson2JavaTypeMapper.mapper - the mapper.setTypeMapper(Jackson2JavaTypeMapper)