Skip to content

Latest commit

 

History

History
171 lines (145 loc) · 7.35 KB

README.md

File metadata and controls

171 lines (145 loc) · 7.35 KB

Java Spring generator

All Contributors

Use your AsyncAPI definition to generate java code to subscribe and publish messages

Usage

AsyncAPI definitions

In order for the generator to know what names to use for some methods it's necessary to make use of AsyncAPI specification bindings. here is an example of how to use it:

channels:
  event.lighting.measured:
    publish:
      bindings:
        kafka:
          groupId: my-group
      message:
         $ref: '#/components/messages/lightMeasured'
    subscribe:
      message:
        $ref: '#/components/messages/lightMeasured'

here is a complete example

asyncapi: '2.0.0'
info:
  title: Streetlights API
  version: '1.0.0'
  description: |
    The Smartylighting Streetlights API allows you
    to remotely manage the city lights.
  license:
    name: Apache 2.0
    url: 'https://www.apache.org/licenses/LICENSE-2.0'

servers:
  production:
    url: api.streetlights.smartylighting.com:{port}
    protocol: mqtt
    variables:
      port:
        default: '1883'
        enum:
          - '1883'
          - '8883'

channels:
  event.lighting.measured:
    publish:
      bindings:
        kafka:
          groupId: my-group
      message:
        $ref: '#/components/messages/lightMeasured'
    subscribe:
      message:
        $ref: '#/components/messages/lightMeasured'
components:
  messages:
    lightMeasured:
      summary: Inform about environmental lighting conditions for a particular streetlight.
      payload:
        $ref: "#/components/schemas/lightMeasuredPayload"
  schemas:
    lightMeasuredPayload:
      type: object
      properties:
        lumens:
          type: integer
          minimum: 0
          description: Light intensity measured in lumens.
        sentAt:
          $ref: "#/components/schemas/sentAt"
    sentAt:
      type: string
      format: date-time
      description: Date and time when the message was sent.

From the command-line interface (CLI)

  Usage: ag [options] <asyncapi> @asyncapi/java-spring-template

  Options:

    -V, --version                 output the version number
    -o, --output <outputDir>       directory where to put the generated files (defaults to current directory)
    -p, --param <name=value>       additional param to pass to templates
    -h, --help                    output usage information

Supported parameters

Name Description Required Default
disableEqualsHashCode Disable generation of equals and hashCode methods for model classes. No false
inverseOperations Generate an application that will publish messages to publish operation of channels and read messages from subscribe operation of channels. Literally this flag will simply swap publish and subscribe operations in the channels.
This flag will be useful when you want to generate a code of mock for your main application. Be aware, generation could be incomplete and manual changes will be required e.g. if bindings are defined only for case of main application.
No false
listenerPollTimeout Only for Kafka. Timeout in ms to use when polling the consumer. No 3000
listenerConcurrency Only for Kafka. Number of threads to run in the listener containers. No 3
asyncapiFileDir Path where original AsyncAPI file will be stored. No src/main/resources/api/
javaPackage The Java package of the generated classes. Alternatively you can set the specification extension info.x-java-package. If both extension and parameter are used, parameter has more priority. No com.asyncapi

Examples

The shortest possible syntax:

ag asyncapi.yaml @asyncapi/java-spring-template

Specify where to put the result and define poll timeout:

ag -o ./src asyncapi.yaml -p listenerPollTimeout=5000 @asyncapi/java-spring-template

If you don't have the AsyncAPI Generator installed, you can install it like this:

npm install -g @asyncapi/generator

Run it

Go to the root folder of the generated code and run this command (you need the JDK1.8):

./gradlew bootRun

Generated source contains RabbitMQ docker-compose. So you could use it to test amqp with:

docker-compose -f src/main/docker/rabbitmq.yml up -d

Missing features

See the list of features that are still missing in the component:

  • support of Kafka is done based on clear "spring-kafka" library without integration like for mqtt or amqp
  • generated code for protocols mqtt and amqp could be out of date. Please have a look to application.yaml and AmqpConfig.java, MqttConfig.java
  • tests are not provided
  • add annotation to the model generation. Consider "@Valid", "@JsonProperty", "@Size", "@NotNull" e.t.c.
  • parameters for topics are not supported
  • server variables are not entirely supported
  • security schemas are not supported
  • traits are not supported
  • Json serializer/desirializer is used always, without taking into account real content type
  • client side generation mode (in general just flip subscribe and publish channels)
  • template generation of docker-compose depending on protocol of server, now the rabbitmq is hardcoded

If you want to help us develop them, feel free to contribute.

Contributors ✨

Thanks goes to these wonderful people (emoji key):


Semen

📖 💻

Francesco Nobilia

👀

This project follows the all-contributors specification. Contributions of any kind welcome!