Class DefaultKafkaHeaderMapper
- java.lang.Object
- 
- org.springframework.kafka.support.AbstractKafkaHeaderMapper
- 
- org.springframework.kafka.support.DefaultKafkaHeaderMapper
 
 
- 
- All Implemented Interfaces:
- KafkaHeaderMapper
 
 public class DefaultKafkaHeaderMapper extends AbstractKafkaHeaderMapper Default header mapper for Apache Kafka. Most headers inKafkaHeadersare not mapped on outbound messages. The exceptions are correlation and reply headers for request/reply messaging. Header types are added to a special headerJSON_TYPES.- Since:
- 1.3
- Author:
- Gary Russell, Artem Bilan
 
- 
- 
Nested Class SummaryNested Classes Modifier and Type Class Description static classDefaultKafkaHeaderMapper.NonTrustedHeaderTypeRepresents a header that could not be decoded due to an untrusted type.- 
Nested classes/interfaces inherited from class org.springframework.kafka.support.AbstractKafkaHeaderMapperAbstractKafkaHeaderMapper.HeaderMatcher, AbstractKafkaHeaderMapper.NeverMatchHeaderMatcher, AbstractKafkaHeaderMapper.SimplePatternBasedHeaderMatcher
 
- 
 - 
Field SummaryFields Modifier and Type Field Description static java.lang.StringJSON_TYPESHeader name for java types of other headers.- 
Fields inherited from class org.springframework.kafka.support.AbstractKafkaHeaderMapperlogger
 
- 
 - 
Constructor SummaryConstructors Constructor Description DefaultKafkaHeaderMapper()Construct an instance with the default object mapper and default header patterns for outbound headers; all inbound headers are mapped.DefaultKafkaHeaderMapper(com.fasterxml.jackson.databind.ObjectMapper objectMapper)Construct an instance with the provided object mapper and default header patterns for outbound headers; all inbound headers are mapped.DefaultKafkaHeaderMapper(com.fasterxml.jackson.databind.ObjectMapper objectMapper, java.lang.String... patterns)Construct an instance with the provided object mapper and the provided header patterns for outbound headers; all inbound headers are mapped.DefaultKafkaHeaderMapper(java.lang.String... patterns)Construct an instance with a default object mapper and the provided header patterns for outbound headers; all inbound headers are mapped.
 - 
Method SummaryAll Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddToStringClasses(java.lang.String... classNames)Add class names that the outbound mapper should perform toString() operations on before mapping.voidaddTrustedPackages(java.lang.String... packagesToTrust)Add packages to the trusted packages list (defaultjava.util, java.lang) used when constructing objects from JSON.voidfromHeaders(org.springframework.messaging.MessageHeaders headers, org.apache.kafka.common.header.Headers target)Map from the givenMessageHeadersto the specified target message.protected com.fasterxml.jackson.databind.ObjectMappergetObjectMapper()Return the object mapper.protected java.util.Set<java.lang.String>getToStringClasses()Provide direct access to the toString() classes by subclasses.protected java.util.Set<java.lang.String>getTrustedPackages()Provide direct access to the trusted packages set for subclasses.protected booleanisEncodeStrings()voidsetEncodeStrings(boolean encodeStrings)Set to true to encode String-valued headers as JSON ("..."), by default just the raw String value is converted to a byte array using the configured charset.voidtoHeaders(org.apache.kafka.common.header.Headers source, java.util.Map<java.lang.String,java.lang.Object> headers)Map from the given target message to abstractedMessageHeaders.protected booleantrusted(java.lang.String requestedType)- 
Methods inherited from class org.springframework.kafka.support.AbstractKafkaHeaderMapperaddMatchers, addRawMappedHeader, getCharset, headerValueToAddIn, headerValueToAddOut, matches, matches, setCharset, setMapAllStringsOut, setRawMappedHeaders
 
- 
 
- 
- 
- 
Field Detail- 
JSON_TYPESpublic static final java.lang.String JSON_TYPES Header name for java types of other headers.- See Also:
- Constant Field Values
 
 
- 
 - 
Constructor Detail- 
DefaultKafkaHeaderMapperpublic DefaultKafkaHeaderMapper() Construct an instance with the default object mapper and default header patterns for outbound headers; all inbound headers are mapped. The default pattern list is"!id", "!timestamp" and "*". In addition, most of the headers inKafkaHeadersare never mapped as headers since they represent data in consumer/producer records.- See Also:
- DefaultKafkaHeaderMapper(ObjectMapper)
 
 - 
DefaultKafkaHeaderMapperpublic DefaultKafkaHeaderMapper(com.fasterxml.jackson.databind.ObjectMapper objectMapper) Construct an instance with the provided object mapper and default header patterns for outbound headers; all inbound headers are mapped. The patterns are applied in order, stopping on the first match (positive or negative). Patterns are negated by preceding them with "!". The default pattern list is"!id", "!timestamp" and "*". In addition, most of the headers inKafkaHeadersare never mapped as headers since they represent data in consumer/producer records.- Parameters:
- objectMapper- the object mapper.
- See Also:
- PatternMatchUtils.simpleMatch(String, String)
 
 - 
DefaultKafkaHeaderMapperpublic DefaultKafkaHeaderMapper(java.lang.String... patterns) Construct an instance with a default object mapper and the provided header patterns for outbound headers; all inbound headers are mapped. The patterns are applied in order, stopping on the first match (positive or negative). Patterns are negated by preceding them with "!". The patterns will replace the default patterns; you generally should not map the"id" and "timestamp"headers. Note: most of the headers inKafkaHeadersare ever mapped as headers since they represent data in consumer/producer records.- Parameters:
- patterns- the patterns.
- See Also:
- PatternMatchUtils.simpleMatch(String, String)
 
 - 
DefaultKafkaHeaderMapperpublic DefaultKafkaHeaderMapper(com.fasterxml.jackson.databind.ObjectMapper objectMapper, java.lang.String... patterns)Construct an instance with the provided object mapper and the provided header patterns for outbound headers; all inbound headers are mapped. The patterns are applied in order, stopping on the first match (positive or negative). Patterns are negated by preceding them with "!". The patterns will replace the default patterns; you generally should not map the"id" and "timestamp"headers. Note: most of the headers inKafkaHeadersare never mapped as headers since they represent data in consumer/producer records.- Parameters:
- objectMapper- the object mapper.
- patterns- the patterns.
- See Also:
- PatternMatchUtils.simpleMatch(String, String)
 
 
- 
 - 
Method Detail- 
getObjectMapperprotected com.fasterxml.jackson.databind.ObjectMapper getObjectMapper() Return the object mapper.- Returns:
- the mapper.
 
 - 
getTrustedPackagesprotected java.util.Set<java.lang.String> getTrustedPackages() Provide direct access to the trusted packages set for subclasses.- Returns:
- the trusted packages.
- Since:
- 2.2
 
 - 
getToStringClassesprotected java.util.Set<java.lang.String> getToStringClasses() Provide direct access to the toString() classes by subclasses.- Returns:
- the toString() classes.
- Since:
- 2.2
 
 - 
isEncodeStringsprotected boolean isEncodeStrings() 
 - 
setEncodeStringspublic void setEncodeStrings(boolean encodeStrings) Set to true to encode String-valued headers as JSON ("..."), by default just the raw String value is converted to a byte array using the configured charset. Set to true if a consumer of the outbound record is using Spring for Apache Kafka version less than 2.3- Parameters:
- encodeStrings- true to encode (default false).
- Since:
- 2.3
 
 - 
addTrustedPackagespublic void addTrustedPackages(java.lang.String... packagesToTrust) Add packages to the trusted packages list (defaultjava.util, java.lang) used when constructing objects from JSON. If any of the supplied packages is"*", all packages are trusted. If a class for a non-trusted package is encountered, the header is returned to the application with value of typeDefaultKafkaHeaderMapper.NonTrustedHeaderType.- Parameters:
- packagesToTrust- the packages to trust.
 
 - 
addToStringClassespublic void addToStringClasses(java.lang.String... classNames) Add class names that the outbound mapper should perform toString() operations on before mapping.- Parameters:
- classNames- the class names.
- Since:
- 2.2
 
 - 
fromHeaderspublic void fromHeaders(org.springframework.messaging.MessageHeaders headers, org.apache.kafka.common.header.Headers target)Description copied from interface:KafkaHeaderMapperMap from the givenMessageHeadersto the specified target message.- Parameters:
- headers- the abstracted MessageHeaders.
- target- the native target message.
 
 - 
toHeaderspublic void toHeaders(org.apache.kafka.common.header.Headers source, java.util.Map<java.lang.String,java.lang.Object> headers)Description copied from interface:KafkaHeaderMapperMap from the given target message to abstractedMessageHeaders.- Parameters:
- source- the native target message.
- headers- the target headers.
 
 - 
trustedprotected boolean trusted(java.lang.String requestedType) 
 
- 
 
-