2

I have a problem deserialising messages which have been posted to a Kafka topic. I'm using spring-boot, spring cloud stream, apache kafka, and apache avro. The object I am trying to deserialise has a constructor which takes in two args. I am getting an exception when taking a message from the topic:

org.springframework.messaging.MessagingException: Exception thrown while invoking com.foo.bar.messaging.ResponseListener#handleResponse[1 args]; 
nested exception is java.lang.RuntimeException: java.lang.NoSuchMethodException: com.foo.bar.TheObject.<init>()     at org.springframework.cloud.stream.binding.StreamListenerMessageHandler.handleRequestMessage(StreamListenerMessageHandler.java:63) ~[spring-cloud-stream-2.0.1.RELEASE.jar:2.0.1.RELEASE]

The class which is being deserialised has a constructor which takes 2 args, and looks like this:

public class TheObject {
    private final int thingOne;
    private final int thingTwo;
    @JsonCreator
    public TheObject(@JsonProperty("thingOne") int thingOne, @JsonProperty("thingTwo") int thingTwo) {
      this.thingOne = thingOne;
      this.thingTwo = thingTwo;
    }
}

Previously I used Jackson, and just posted Strings to the kafka topic, so the @JsonCreator annotation was enough to tell the mapper how to construct the object, but I'm now changing to use Arvo. Jackson is no longer used at all for this serialisation. I have left the annotations on the example above to show how it used to work.

In terms of configuration, I am using a property: dynamicSchemaGenerationEnabled: true and not explicitly declaring object schemas. This seems to be working fine with both the spring Schema Registry, and Confluent Schema Registry (apart from the problem described here)

Is there an equivalent to the @JsonCreator annotation that will work with the Arvo deserialisation?

thanks

robjwilkins
  • 5,462
  • 5
  • 43
  • 59
  • for jackson you need the default empty constructor see https://stackoverflow.com/a/50736606/3224238 – Paizo Nov 19 '18 at 12:02
  • 1
    @Paizo that is not true. You do not need a 'default empty constructor' for Jackson. Jackson will already deserialise this class. The problem is that I am now using Avro, which does not use Jackson, therefore my deserialisation is no longer working – robjwilkins Nov 19 '18 at 12:09
  • Jackson supports many formats, actually. https://github.com/FasterXML/jackson-dataformats-binary/blob/master/avro/README.md But Spring Kafka has a separate class/config for the Avro schema registry, and it's not clear if you're using that – OneCricketeer Nov 19 '18 at 14:00
  • 1
    I am no longer using Jackson at all. I was previously, but it has been removed. I left the annotations on the example as I want to continue using the same constructor. I have not explicitly declared a schema - I have set a property: dynamicSchemaGenerationEnabled: true – robjwilkins Nov 19 '18 at 15:17

0 Answers0