Class JsonSerializer<T>
- java.lang.Object
-
- org.springframework.kafka.support.serializer.JsonSerializer<T>
-
- Type Parameters:
T
- class of the entity, representing messages
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
,org.apache.kafka.common.serialization.Serializer<T>
public class JsonSerializer<T> extends java.lang.Object implements org.apache.kafka.common.serialization.Serializer<T>
GenericSerializer
for sending Java objects to Kafka as JSON.- Author:
- Igor Stepanov, Artem Bilan, Gary Russell, Elliot Kennedy
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
ADD_TYPE_INFO_HEADERS
Kafka config property for disabling adding type headers.protected boolean
addTypeInfo
protected com.fasterxml.jackson.databind.ObjectMapper
objectMapper
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
-
Constructor Summary
Constructors Constructor Description JsonSerializer()
JsonSerializer(com.fasterxml.jackson.core.type.TypeReference<? super T> targetType)
JsonSerializer(com.fasterxml.jackson.core.type.TypeReference<? super T> targetType, com.fasterxml.jackson.databind.ObjectMapper objectMapper)
JsonSerializer(com.fasterxml.jackson.databind.JavaType targetType, com.fasterxml.jackson.databind.ObjectMapper objectMapper)
JsonSerializer(com.fasterxml.jackson.databind.ObjectMapper objectMapper)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
void
configure(java.util.Map<java.lang.String,?> configs, boolean isKey)
<X> JsonSerializer<X>
copyWithType(com.fasterxml.jackson.core.type.TypeReference<? super X> newTargetType)
Copies this serializer with same configuration, except new target type reference is used.<X> JsonSerializer<X>
copyWithType(com.fasterxml.jackson.databind.JavaType newTargetType)
Copies this serializer with same configuration, except new target java type is used.<X> JsonSerializer<X>
copyWithType(java.lang.Class<? super X> newTargetType)
Copies this serializer with same configuration, except new target type reference is used.protected static java.util.Map<java.lang.String,java.lang.Class<?>>
createMappings(java.lang.String mappings)
JsonSerializer<T>
forKeys()
Designate this serializer for serializing keys (default is values); only applies if the default type mapper is used.Jackson2JavaTypeMapper
getTypeMapper()
boolean
isAddTypeInfo()
JsonSerializer<T>
noTypeInfo()
Do not include type info headers.byte[]
serialize(java.lang.String topic, org.apache.kafka.common.header.Headers headers, T data)
byte[]
serialize(java.lang.String topic, T data)
void
setAddTypeInfo(boolean addTypeInfo)
Set to false to disable adding type info headers.void
setTypeMapper(Jackson2JavaTypeMapper typeMapper)
Set a customized type mapper.void
setUseTypeMapperForKey(boolean isKey)
Configure the default Jackson2JavaTypeMapper to use key type headers.JsonSerializer<T>
typeMapper(Jackson2JavaTypeMapper mapper)
Use the suppliedJackson2JavaTypeMapper
.
-
-
-
Field Detail
-
ADD_TYPE_INFO_HEADERS
public static final java.lang.String ADD_TYPE_INFO_HEADERS
Kafka config property for disabling adding type headers.- See Also:
- Constant Field Values
-
TYPE_MAPPINGS
public 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
-
objectMapper
protected final com.fasterxml.jackson.databind.ObjectMapper objectMapper
-
addTypeInfo
protected boolean addTypeInfo
-
typeMapper
protected Jackson2JavaTypeMapper typeMapper
-
-
Constructor Detail
-
JsonSerializer
public JsonSerializer()
-
JsonSerializer
public JsonSerializer(com.fasterxml.jackson.core.type.TypeReference<? super T> targetType)
-
JsonSerializer
public JsonSerializer(com.fasterxml.jackson.databind.ObjectMapper objectMapper)
-
JsonSerializer
public JsonSerializer(com.fasterxml.jackson.core.type.TypeReference<? super T> targetType, com.fasterxml.jackson.databind.ObjectMapper objectMapper)
-
JsonSerializer
public JsonSerializer(com.fasterxml.jackson.databind.JavaType targetType, com.fasterxml.jackson.databind.ObjectMapper objectMapper)
-
-
Method Detail
-
isAddTypeInfo
public boolean isAddTypeInfo()
-
setAddTypeInfo
public void setAddTypeInfo(boolean addTypeInfo)
Set to false to disable adding type info headers.- Parameters:
addTypeInfo
- true to add headers.- Since:
- 2.1
-
getTypeMapper
public Jackson2JavaTypeMapper getTypeMapper()
-
setTypeMapper
public void setTypeMapper(Jackson2JavaTypeMapper typeMapper)
Set a customized type mapper.- Parameters:
typeMapper
- the type mapper.- Since:
- 2.1
-
setUseTypeMapperForKey
public 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
-
configure
public void configure(java.util.Map<java.lang.String,?> configs, boolean isKey)
- Specified by:
configure
in interfaceorg.apache.kafka.common.serialization.Serializer<T>
-
createMappings
protected static java.util.Map<java.lang.String,java.lang.Class<?>> createMappings(java.lang.String mappings)
-
serialize
@Nullable public byte[] serialize(java.lang.String topic, org.apache.kafka.common.header.Headers headers, @Nullable T data)
- Specified by:
serialize
in interfaceorg.apache.kafka.common.serialization.Serializer<T>
-
serialize
@Nullable public byte[] serialize(java.lang.String topic, @Nullable T data)
- Specified by:
serialize
in interfaceorg.apache.kafka.common.serialization.Serializer<T>
-
close
public void close()
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Specified by:
close
in interfaceorg.apache.kafka.common.serialization.Serializer<T>
-
copyWithType
public <X> JsonSerializer<X> copyWithType(java.lang.Class<? super X> newTargetType)
Copies this serializer with same configuration, except new target type reference is used.- Type Parameters:
X
- new serialization source type- Parameters:
newTargetType
- type reference forced for serialization, not null- Returns:
- new instance of serializer with type changes
- Since:
- 2.6
-
copyWithType
public <X> JsonSerializer<X> copyWithType(com.fasterxml.jackson.core.type.TypeReference<? super X> newTargetType)
Copies this serializer with same configuration, except new target type reference is used.- Type Parameters:
X
- new serialization source type- Parameters:
newTargetType
- type reference forced for serialization, not null- Returns:
- new instance of serializer with type changes
- Since:
- 2.6
-
copyWithType
public <X> JsonSerializer<X> copyWithType(com.fasterxml.jackson.databind.JavaType newTargetType)
Copies this serializer with same configuration, except new target java type is used.- Type Parameters:
X
- new serialization source type- Parameters:
newTargetType
- java type forced for serialization, not null- Returns:
- new instance of serializer with type changes
- Since:
- 2.6
-
forKeys
public JsonSerializer<T> forKeys()
Designate this serializer for serializing keys (default is values); only applies if the default type mapper is used.- Returns:
- the serializer.
- Since:
- 2.3
- See Also:
setUseTypeMapperForKey(boolean)
-
noTypeInfo
public JsonSerializer<T> noTypeInfo()
Do not include type info headers.- Returns:
- the serializer.
- Since:
- 2.3
- See Also:
setAddTypeInfo(boolean)
-
typeMapper
public JsonSerializer<T> typeMapper(Jackson2JavaTypeMapper mapper)
Use the suppliedJackson2JavaTypeMapper
.- Parameters:
mapper
- the mapper.- Returns:
- the serializer.
- Since:
- 2.3
- See Also:
setTypeMapper(Jackson2JavaTypeMapper)
-
-