简体   繁体   中英

“Malformed data. Length is negative” while deserializing avro class

I use kafka with avro serialization: http://docs.spring.io/spring-cloud-stream/docs/current-SNAPSHOT/reference/htmlsingle/#_avro_schema_registry_client_message_converters

I registered schema in local schema-registry and add ed @EnableSchemaRegistryClient to my application with setting in application.yml :

spring:
  cloud:
    stream:
      bindings:
        output:
          destination: output
        input:
          destination: topicName
          contentType: application/*+avro
      schemaRegistryClient:
        endpoint: http://127.0.0.1:8081

I also generated class using avro-tools-1.8.1.jar . Now I'm trying to read and convert message from kafka:

  @StreamListener(Sink.INPUT)
  public void handlePublish(MyClass message) throws IOException {
    logger.info("Receiving MyClass" + message);
  }

But converter fails with: org.apache.avro.AvroRuntimeException: Malformed data. Length is negative org.apache.avro.AvroRuntimeException: Malformed data. Length is negative .

Stack trace:

 org.springframework.messaging.MessagingException: Exception thrown while invoking kafka.Consumer#handlePublish[1 args]; nested exception is org.apache.avro.AvroRuntimeException: Malformed data. Length is negative: -62
    at org.springframework.cloud.stream.binding.StreamListenerAnnotationBeanPostProcessor$StreamListenerMessageHandler.handleRequestMessage(StreamListenerAnnotationBeanPostProcessor.java:364) ~[spring-cloud-stream-1.1.1.RELEASE.jar:1.1.1.RELEASE]
    ....
Caused by: org.apache.avro.AvroRuntimeException: Malformed data. Length is negative: -62
at org.apache.avro.io.BinaryDecoder.doReadBytes(BinaryDecoder.java:336) ~[avro-1.8.1.jar:1.8.1]
...at org.springframework.cloud.stream.schema.avro.AbstractAvroMessageConverter.convertFromInternal(AbstractAvroMessageConverter.java:91) ~[spring-cloud-stream-schema-1.1.1.RELEASE.jar:1.1.1.RELEASE]
    at org.springframework.messaging.converter.AbstractMessageConverter.fromMessage(AbstractMessageConverter.java:175) ~[spring-messaging-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.messaging.converter.CompositeMessageConverter.fromMessage(CompositeMessageConverter.java:67) ~[spring-messaging-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.messaging.handler.annotation.support.PayloadArgumentResolver.resolveArgument(PayloadArgumentResolver.java:117) ~[spring-messaging-4.3.4.RELEASE.jar:4.3.4.RELEASE]
....

我注意到您上面使用的属性是spring.cloud.schemaRegistryClient但它必须是spring.cloud.stream.schemaRegistryClient

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM