Class BinderHeaderMapper
java.lang.Object
org.springframework.kafka.support.AbstractKafkaHeaderMapper
org.springframework.cloud.stream.binder.kafka.BinderHeaderMapper
- All Implemented Interfaces:
org.springframework.kafka.support.KafkaHeaderMapper
public class BinderHeaderMapper
extends org.springframework.kafka.support.AbstractKafkaHeaderMapper
Custom header mapper for Apache Kafka. This is identical to the
DefaultKafkaHeaderMapper
from spring Kafka. This is provided for addressing some interoperability issues between Spring Cloud Stream 3.0.x
and 2.x apps, where mime types passed as regular MimeType
in the header are not de-serialized properly.
It also suppresses certain internal headers that should never be propagated on output.
Most headers in KafkaHeaders
are not mapped onto outbound messages.
The exceptions are correlation and reply headers for request/reply
messaging.
Header types are added to a special header JSON_TYPES
.- Since:
- 3.0.0
- Author:
- Gary Russell, Artem Bilan, Soby Chacko
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Represents a header that could not be decoded due to an untrusted type.Nested classes/interfaces inherited from class org.springframework.kafka.support.AbstractKafkaHeaderMapper
org.springframework.kafka.support.AbstractKafkaHeaderMapper.HeaderMatcher, org.springframework.kafka.support.AbstractKafkaHeaderMapper.NeverMatchHeaderMatcher, org.springframework.kafka.support.AbstractKafkaHeaderMapper.SimplePatternBasedHeaderMatcher
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
Header name for java types of other headers.Fields inherited from class org.springframework.kafka.support.AbstractKafkaHeaderMapper
logger
-
Constructor Summary
ConstructorDescriptionConstruct an instance with the default object mapper and default header patterns for outbound headers; all inbound headers are mapped.BinderHeaderMapper
(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.BinderHeaderMapper
(com.fasterxml.jackson.databind.ObjectMapper objectMapper, String... patterns) Construct an instance with the provided object mapper and the provided header patterns for outbound headers; all inbound headers are mapped.BinderHeaderMapper
(String... patterns) Construct an instance with a default object mapper and the provided header patterns for outbound headers; all inbound headers are mapped. -
Method Summary
Modifier and TypeMethodDescriptionstatic String[]
addNeverHeaderPatterns
(List<String> patterns) Add patterns for headers that should never be mapped.void
addToStringClasses
(String... classNames) Add class names that the outbound mapper should perform toString() operations on before mapping.void
addTrustedPackages
(String... packagesToTrust) Add packages to the trusted packages list (defaultjava.util, java.lang
) used when constructing objects from JSON.void
fromHeaders
(org.springframework.messaging.MessageHeaders headers, org.apache.kafka.common.header.Headers target) protected com.fasterxml.jackson.databind.ObjectMapper
Return the object mapper.Provide direct access to the toString() classes by subclasses.Provide direct access to the trusted packages set for subclasses.protected boolean
static void
removeNeverHeaders
(org.apache.kafka.common.header.Headers headers) Remove never headers.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.void
protected boolean
Methods inherited from class org.springframework.kafka.support.AbstractKafkaHeaderMapper
addMatchers, addRawMappedHeader, getCharset, headerValueToAddIn, headerValueToAddOut, matches, matches, matchesForInbound, setCharset, setMapAllStringsOut, setRawMappedHeaders
-
Field Details
-
JSON_TYPES
Header name for java types of other headers.- See Also:
-
-
Constructor Details
-
BinderHeaderMapper
public BinderHeaderMapper()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 inKafkaHeaders
are never mapped as headers since they represent data in consumer/producer records.- See Also:
-
BinderHeaderMapper
public BinderHeaderMapper(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 inKafkaHeaders
are never mapped as headers since they represent data in consumer/producer records.- Parameters:
objectMapper
- the object mapper.- See Also:
-
PatternMatchUtils.simpleMatch(String, String)
-
BinderHeaderMapper
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 inKafkaHeaders
are ever mapped as headers since they represent data in consumer/producer records.- Parameters:
patterns
- the patterns.- See Also:
-
PatternMatchUtils.simpleMatch(String, String)
-
BinderHeaderMapper
public BinderHeaderMapper(com.fasterxml.jackson.databind.ObjectMapper objectMapper, 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 inKafkaHeaders
are 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 Details
-
getObjectMapper
protected com.fasterxml.jackson.databind.ObjectMapper getObjectMapper()Return the object mapper.- Returns:
- the mapper.
-
getTrustedPackages
Provide direct access to the trusted packages set for subclasses.- Returns:
- the trusted packages.
- Since:
- 2.2
-
getToStringClasses
Provide direct access to the toString() classes by subclasses.- Returns:
- the toString() classes.
- Since:
- 2.2
-
isEncodeStrings
protected boolean isEncodeStrings() -
setEncodeStrings
public 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
-
addTrustedPackages
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 typeBinderHeaderMapper.NonTrustedHeaderType
.- Parameters:
packagesToTrust
- the packages to trust.
-
addToStringClasses
Add class names that the outbound mapper should perform toString() operations on before mapping.- Parameters:
classNames
- the class names.- Since:
- 2.2
-
fromHeaders
public void fromHeaders(org.springframework.messaging.MessageHeaders headers, org.apache.kafka.common.header.Headers target) -
toHeaders
-
trusted
-
addNeverHeaderPatterns
Add patterns for headers that should never be mapped.- Parameters:
patterns
- the patterns.- Returns:
- the modified patterns.
- Since:
- 3.0.2
-
removeNeverHeaders
public static void removeNeverHeaders(org.apache.kafka.common.header.Headers headers) Remove never headers.- Parameters:
headers
- the headers from which to remove the never headers.- Since:
- 3.0.2
-