问题
Here is the error I get:
org.apache.kafka.common.errors.SerializationException: Error deserializing
key/value for partition distance-0 at offset 0. If needed, please seek past
the record to continue consumption.
Caused by: java.lang.IllegalStateException: No type information in headers
and no default type provided
at org.springframework.util.Assert.state(Assert.java:73)
at org.
springframework.kafka.support.serializer.JsonDeserializer.deserialize(JsonDeserializer.java:370)
And applicaiton-dev.yml:
spring:
json:
use:
type:
headers: false
value:
default:
type: Object
method: com.mycompany.mypackage.KafkaConfig.returnType
Also tried with:
consumer:
bootstrap-servers: 10.10.5.189:9092
value-deserializer: org.springframework.kafka.support.serializer.JsonDeserializer
enable-auto-commit: false
auto-offset-reset: earliest
properties:
spring:
json:
trusted:
packages: '*'
value:
default:
method: com.mycompany.mypackage.KafkaConfig.returnTypee
There were no warning but i put printlin in static method and it never fired? What gives?
@KafkaListener(topics = "distance", groupId = "${kafka.myinfo.id}")
public void handle(CustDeletedEvent custDeletedEvent) {
log.debug("received jsonNode: "+ userDeletedEvent);
KafkaConfig.java
// NEVER CALLED!!!
public static JavaType returnType(byte[] data, Headers headers) {
System.out.println("return type called data.length="+data.length);
JavaType custDeletedEvent =
TypeFactory.defaultInstance().constructType(CustDeletedEvent.class);
return custDeletedEvent;
}
Latest config:
kafka:
bootstrap-servers: 10.10.5.189:9092
producer:
bootstrap-servers: 10.10.5.189:9092
value-serializer: org.springframework.kafka.support.serializer.JsonSerializer
consumer:
bootstrap-servers: 10.10.5.189:9092
value-deserializer: org.springframework.kafka.support.serializer.JsonDeserializer
enable-auto-commit: false
auto-offset-reset: earliest
properties:
spring:
json:
trusted:
packages: '*'
value:
method: mypackage.config.KafkaConfig.returnType
Spring Kafka configured here
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
here is exact yaml:
spring:
profiles:
active: dev
kafka:
bootstrap-servers: 10.10.5.189:9092
producer:
bootstrap-servers: 10.10.5.189:9092
value-serializer: org.springframework.kafka.support.serializer.JsonSerializer
consumer:
bootstrap-servers: 10.10.5.189:9092
value-deserializer: org.springframework.kafka.support.serializer.JsonDeserializer
enable-auto-commit: false
auto-offset-reset: earliest
properties:
spring:
json:
trusted:
packages: '*'
value:
method: com.service.cust.impl.returnType
回答1:
It needs to go in spring.kafka.consumer.properties.spring.json...
.
来源:https://stackoverflow.com/questions/63263947/kafka-spring-deserialzer-returntype-static-method-never-called