"格式错误的数据。长度是负的"而反序列化avro类

时间:2017-02-19 14:35:29

标签: java spring apache-kafka avro spring-cloud-stream

我使用avaf序列化的kafka: http://docs.spring.io/spring-cloud-stream/docs/current-SNAPSHOT/reference/htmlsingle/#_avro_schema_registry_client_message_converters

我在本地架构注册表中注册了架构,并在我的应用程序中添加了@EnableSchemaRegistryClient并设置了application.yml

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

我还使用avro-tools-1.8.1.jar生成了类。现在我试图从kafka读取和转换消息:

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

但是转换器失败了:org.apache.avro.AvroRuntimeException: Malformed data. Length is negative

堆栈追踪:

 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]
....

1 个答案:

答案 0 :(得分:1)

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