javajacksonavro

Erro while converting Java POJO to Avro GenericRecord


I am trying to convert the Java POJO class into Avro format to send the data to the Kafka message broker using KafkaAvroSerializer.

I am facing the below error while converting the Java POJO class to Avro GenericRecord. The error I am facing is

java.lang.NoClassDefFoundError: Failed to link com/fasterxml/jackson/dataformat/avro/AvroParser$Feature (Module "deployment.MyAppModule.ear:main" from Service Module Loader): com/fasterxml/jackson/core/FormatFeature

The code that I am trying to use here is

public static GenericRecord convertPojoToGenericRecord(String schemaPath, MyMessageClass message){
  InputStream inStream = MyAppClass.class.getClassLoader().getResourceAsStream(schemaPath);
  Schema schema = new Schema.Parser().setValidate(true).parse(inStream);
  AvroSchema avSchema = new AvroSchema(schema);
  AvroMapper avroMapper = new AvroMapper();
  final byte[] bytes = avroMapper.writer(avSchema).writeValueAsBytes(message);
  GenericDatumReader<Object> genericRecordReader = new GenericDatumReader<>(avSchema);
  return (GenericRecord) genericRecordReader.read(null, DecoderFactory.get().binaryDecoder(bytes, null));
 } 

The exception is coming while executing below statement

AvroMapper avroMapper = new AvroMapper();

The maven dependencies that I am using here are

jackson-dataformat-avro
jackson-core
jackson-databind
jackson-annotations
jackson-jars-json-provider
jackson-datatype-joda

The version for all the above dependencies is 2.9.10.

The application is a legacy application. I cannot change or upgrade the version for the Jackson libraries as these libraries are being used in other modules.

The application is getting deployed into JBoss server (version 10).

When I tried to get more details about the dependency tree, I found the jackson-dataformat-avro is using old dependency of jackson-databind with version 2.5.4

Any help related to this is appreciated.

Thanks, Avinash


Solution

  • Since, your application legacy one, you can create one library that converts JSON to different avro schema using gradle-avro-plugin framework.
    Then you can integrate the library with your app. I think this is better and more flexible.