Class AbstractKafkaHeaderMapper
java.lang.Object
org.springframework.kafka.support.AbstractKafkaHeaderMapper
- All Implemented Interfaces:
KafkaHeaderMapper
- Direct Known Subclasses:
DefaultKafkaHeaderMapper, JsonKafkaHeaderMapper, SimpleKafkaHeaderMapper
Base for Kafka header mappers.
- Since:
- 2.1.3
- Author:
- Gary Russell, Artem Bilan, Sanghyeok An, Soby Chacko, Ngoc Nhan
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static interfaceA matcher for headers.protected static classA matcher that never matches a set of headers.protected static classA pattern-based header matcher that matches if the specified header matches the specified simple pattern. -
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractKafkaHeaderMapper(boolean outbound, String... patterns) Construct a mapper that will match the supplied patterns (outbound) and all headers (inbound).AbstractKafkaHeaderMapper(String... patterns) Construct a mapper that will match the supplied patterns (outbound) and all headers (inbound). -
Method Summary
Modifier and TypeMethodDescriptionprotected final voidaddMatchers(AbstractKafkaHeaderMapper.HeaderMatcher... matchersToAdd) Subclasses can invoke this to add customAbstractKafkaHeaderMapper.HeaderMatchers.voidaddRawMappedHeader(String name, boolean toString) Add a raw mapped header.protected booleandoesMatchMultiValueHeader(String headerName) Determine whether the given header name should be mapped to multiple values.protected voidAdd the already converted header value to the target headers, collecting values for headers matching a multi-value pattern into aList.protected voidfromUserHeader(String headerName, org.apache.kafka.common.header.Header header, Map<String, Object> headers) Handle non-reserved headers inDefaultKafkaHeaderMapper.protected Charsetprotected @Nullable ObjectheaderValueToAddIn(@Nullable org.apache.kafka.common.header.Header header) Check if the header value should be mapped to a String, if so configured.protected ObjectheaderValueToAddOut(String key, Object value) Check if the value is a String and convert to byte[], if so configured.protected booleanprotected booleanprotected booleanmatchesForInbound(String header) Matches header names for inbound mapping when configured as an inbound mapper.voidsetCharset(Charset charset) Set the charset to use when mapping String-valued headers to/from byte[].voidsetMapAllStringsOut(boolean mapAllStringsOut) Set to true to map allStringvalued outbound headers tobyte[].voidsetMultiValueHeaderPatterns(String... patterns) Add patterns for matching multi-value headers under the same key.voidsetRawMappedHeaders(Map<String, Boolean> rawMappedHeaders) Set the headers to not perform any conversion on (exceptStringtobyte[]for outbound).Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface KafkaHeaderMapper
fromHeaders, toHeaders
-
Field Details
-
logger
-
-
Constructor Details
-
AbstractKafkaHeaderMapper
Construct a mapper that will match the supplied patterns (outbound) and all headers (inbound). For outbound mapping, certain internal framework headers are never mapped.- Parameters:
patterns- the patterns.
-
AbstractKafkaHeaderMapper
Construct a mapper that will match the supplied patterns (outbound) and all headers (inbound). For outbound mapping, certain internal framework headers are never mapped.- Parameters:
outbound- true for an outbound mapper.patterns- the patterns.
-
-
Method Details
-
addMatchers
Subclasses can invoke this to add customAbstractKafkaHeaderMapper.HeaderMatchers.- Parameters:
matchersToAdd- the matchers to add.- Since:
- 2.3
-
setMapAllStringsOut
public void setMapAllStringsOut(boolean mapAllStringsOut) Set to true to map allStringvalued outbound headers tobyte[]. To map to aStringfor inbound, there must be an entry in the rawMappedHeaders map.- Parameters:
mapAllStringsOut- true to map all strings.- Since:
- 2.2.5
- See Also:
-
getCharset
-
setCharset
Set the charset to use when mapping String-valued headers to/from byte[]. Default UTF-8.- Parameters:
charset- the charset.- Since:
- 2.2.5
- See Also:
-
setRawMappedHeaders
Set the headers to not perform any conversion on (exceptStringtobyte[]for outbound). Inbound headers that match will be mapped asbyte[]unless the corresponding boolean in the map value is true, in which case it will be mapped as a String. Headers mapped by default are:kafka_listenerInfo,b3,traceparent,X-B3-TraceId,X-B3-SpanId,X-B3-SampledandX-B3-Flags.- Parameters:
rawMappedHeaders- the header names to not convert and- Since:
- 2.2.5
- See Also:
-
addRawMappedHeader
Add a raw mapped header.- Parameters:
name- the header name.toString- convert to string on inbound when true.- Since:
- 2.7.1
- See Also:
-
setMultiValueHeaderPatterns
Add patterns for matching multi-value headers under the same key.- Parameters:
patterns- the patterns for header.- Since:
- 4.0
-
matches
-
matches
-
matchesForInbound
Matches header names for inbound mapping when configured as an inbound mapper.- Parameters:
header- the header name.- Returns:
- true if it can be mapped.
- Since:
- 2.8.8
-
headerValueToAddOut
-
doesMatchMultiValueHeader
Determine whether the given header name should be mapped to multiple values. This method first checks if the mapping result is already cached. If a cached result exists, it is returned immediately. If not,doesMatchInternal(headerName)is called to compute the result, which is then cached and returned.- Parameters:
headerName- the header name.- Returns:
- True for multiple values at the same key.
- Since:
- 4.0
-
fromUserHeader
protected void fromUserHeader(String headerName, org.apache.kafka.common.header.Header header, Map<String, Object> headers) Handle non-reserved headers inDefaultKafkaHeaderMapper.- Parameters:
headerName- the header name.header- the header instance.headers- the target headers.- Since:
- 4.0
-
fromUserHeader
protected void fromUserHeader(String headerName, @Nullable Object headerValue, Map<String, Object> headers) Add the already converted header value to the target headers, collecting values for headers matching a multi-value pattern into aList.- Parameters:
headerName- the header name.headerValue- the converted header value.headers- the target headers.- Since:
- 4.2
-
headerValueToAddIn
protected @Nullable Object headerValueToAddIn(@Nullable org.apache.kafka.common.header.Header header) Check if the header value should be mapped to a String, if so configured.- Parameters:
header- the header.- Returns:
- the value to add.
-