Kafka Spring Deserialzer returnType static method never called

蓝咒 提交于 2020-08-09 14:58:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!