AsyncApi how to generate POJO classes based on AsyncAPI yaml file using Java and maven

693 Views Asked by At

Good morning.

I'm working on a spring boot projet with kafka integration already implemented (a kafka message producer). The kafka configuration is done in application.properties as followed:

spring.kafka.bootstrap-servers=
spring.kafka.properties.sasl.jaas.config= 
spring.kafka.properties.security.protocol= 
spring.kafka.properties.sasl.mechanism= 
spring.kafka.producer.key-serializer=
spring.kafka.producer.value-serializer=
spring.kafka.producer.acks=

Now we are introducing the AsyncAPI to better describe and document our event-driven architecture.

I'm wondering how I can import the asyncApi.yaml(containing the Kafka description) file in my java code.

Please can you advise me how asyncApi.yaml file can be used to implement a kafka producer. Is it possible to generate pojo classes using the asyncApi.yaml ?

I'm using Springboot and maven and i'm not find a proper way to integrate the asyncApi.yaml in the project

thanks a lot.

3

There are 3 best solutions below

1
Lukasz Gornicki On

There is a library that understands AsyncAPI, extracts schemas from it and can generate models in different languages, Java included -> https://github.com/asyncapi/modelina

It is integrated in AsyncAPI CLI that you can use to generate these models, like asyncapi generate models java asyncapi.yml -> https://github.com/asyncapi/cli/blob/master/docs/usage.md#asyncapi-generate-models-language-file

AsyncAPI CLI is not only shipped as NPM package, but also a binary that you could invoke in Maven. Sorry but I do not have an example Maven config that I could share with you. But yeah, you have many different options, like just docker -> https://www.asyncapi.com/docs/tools/cli/installation

0
jonaslagoni On

Following up on Lukasz's response, in the ideal world the CLI would be all you would need. Unfortunately, as long as #780 has not been solved, it will always be limiting.

I have added a Maven example of how to integrate Modelina or the CLI, into the build process, to always have up-to-date POJO classes from the AsyncAPI document. Check it out here: https://github.com/asyncapi/modelina/tree/master/examples/integrate-modelina-into-maven/maven-project

Here how it works:

  • The script ./scripts/modelina/generate.ts is what generates all the models, and what Maven uses to generate the models. This can also be executed manually through npm run generate.
  • The input, in this case, is an AsyncAPI document located in the root of the project ./asyncapi.json. The input can be anything, just alter the generator script.
  • The Maven project file ./pom.xml then utilizes the frontend-maven-plugin to execute the generate script on build so you will always have the up to date models from your AsyncAPI document.

If you would rather execute the CLI, you could remove the script and use the plugin to execute it as an NPX command: https://github.com/eirslett/frontend-maven-plugin#npx

But the classes are not well generated. For message lightMeasured, i'm expecting 3 generated classes: on classe for Header, one classe for lightMeasuredPayload, and one main class called lightMeasuredMessage (having Header and payload as properties). But only the Payload class is generated. I'm i missing something in my command?

@blaiso you are not missing anything, it's simply because the tool only generates the payloads of the message, NOT an object for the message itself. Please head over to https://github.com/asyncapi/modelina/issues/1574 and voice that you need it as well.

You could easily implement this in the generate script, by manually parsing the document and building your own meta models.

0
Stav Shamir On

You can also choose to go the other way around with a code-first approach, and generate an AsyncAPI file from your already existing Spring Boot project with minimal configuration using Springwolf.