1.5.1 • Published 5 months ago

@asyncapi/java-spring-template v1.5.1

Weekly downloads
12
License
Apache-2.0
Repository
github
Last release
5 months ago

AsyncAPI Java Spring Template

Java Spring template for the AsyncAPI Generator.


License npm All Contributors

downloads


Attention, AsyncAPI v3 is not currently supported by this template


Usage

Install AsyncAPI CLI, for details follow the guide.

npm install -g @asyncapi/cli

Generate using CLI.

asyncapi generate fromTemplate <asyncapi.yaml> @asyncapi/java-spring-template

You can replace <asyncapi.yaml> with local path or URL pointing to any AsyncAPI document.

AsyncAPI definitions

To have correctly generated code, your AsyncAPI file MUST define operationId for every operation.

In order for the generator to know what names to use for some parameters AsyncAPI specification bindings SHOULD be used.

It is RECOMMENDED to not use anonymous objects in payload and components definition, if changing of data model is not possible, you MAY use $id to set name of element.

  • Complete example for Kafka is here. Notice information about binding.
    channels:
      event.lighting.measured:
        publish:
          bindings:
            kafka:
              groupId: my-group
          message:
            $ref: '#/components/messages/lightMeasured'
        subscribe:
          message:
            $ref: '#/components/messages/lightMeasured'
  • Complete example for MQTT is here.

Supported parameters

NameDescriptionRequiredDefault
disableEqualsHashCodeDisable generation of equals and hashCode methods for model classes.Nofalse
inverseOperationsGenerate 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.Nofalse
javaPackageThe 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.Nocom.asyncapi
springBoot2Generate template files for the Spring Boot version 2. For kafka protocol it will also force to use spring-kafka 2.9.9Nofalse
mavenGenerate pom.xml Maven build file instead of Gradle build.Nofalse
listenerPollTimeoutOnly for Kafka. Timeout in ms to use when polling the consumer.No3000
listenerConcurrencyOnly for Kafka. Number of threads to run in the listener containers.No3
addTypeInfoHeaderOnly for Kafka. Add type information to message header.Notrue
connectionTimeoutOnly for MQTT. This value, measured in seconds, defines the maximum time interval the client will wait for the network connection to the MQTT server to be established. The default timeout is 30 seconds. A value of 0 disables timeout processing meaning the client will wait until the network connection is made successfully or fails.No30
disconnectionTimeoutOnly for MQTT. The completion timeout in milliseconds when disconnecting. The default disconnect completion timeout is 5000 milliseconds.No5000
completionTimeoutOnly for MQTT. The completion timeout in milliseconds for operations. The default completion timeout is 30000 milliseconds.No30000
mqttClientIdOnly for MQTT. Provides the client identifier for the MQTT server. This parameter overrides the value of the clientId if it's set in the AsyncAPI file.If both aren't provided, a default value is set.No
asyncapiFileDirPath where original AsyncAPI file will be stored.Nosrc/main/resources/api/

Examples

The shortest possible syntax:

asyncapi generate fromTemplate asyncapi.yaml @asyncapi/java-spring-template

Specify where to put the result with -o option and define parameter of poll timeout with -p option:

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

Run it

Go to the root folder of the generated code and run this command (you need the JDK 17):

./gradlew bootRun

Development

  1. Clone the repository:
    git clone https://github.com/asyncapi/java-spring-template
    cd java-spring-template
  2. Download all template dependencies:
    npm install
  3. Make required changes in the template.
  4. Run snapshot tests:
    npm test
    If there falling tests examine diff report and make an appropriate changes in template files or snapshots.
  5. Check output generation project. Install AsyncAPI Generator:
    npm install -g @asyncapi/cli
  6. Run generation (assuming you are in template folder):

    # for MQTT protocol test with below
    asyncapi generate fromTemplate tests/mocks/mqtt.yml ./ -o output
    # for Kafka protocol test with below
    asyncapi generate fromTemplate tests/mocks/kafka.yml ./ -o output
  7. Explore generated files in output directory. Generated project shouldn't contain syntax or compilation errors. Preferably generated tests should pass.

For local development, you need different variations of this command. First of all, you need to know about three important CLI flags:

  • --debug enables the debug mode.
  • --watch-template enables a watcher of changes that you make in the template. It regenerates your template whenever it detects a change.
  • --install enforces reinstallation of the template.

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 protocol amqp could be out of date. Please have a look to application.yaml and AmqpConfig.java
  • tests for protocol amqp are not provided
  • parameters for topics are not supported
  • server variables are not entirely supported
  • security schemas are not supported
  • traits are not supported
  • Json serializer/deserializer is used always, without taking into account real content type
  • 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):

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

1.5.1

5 months ago

1.5.0

5 months ago

1.2.0

7 months ago

1.0.0

8 months ago

1.4.0

7 months ago

1.1.0

7 months ago

1.3.0

7 months ago

0.29.0

11 months ago

0.28.0

2 years ago

0.27.0

2 years ago

0.26.0

2 years ago

0.25.2

2 years ago

0.25.1

2 years ago

0.25.0

2 years ago

0.24.0

3 years ago

0.23.3

3 years ago

0.23.2

3 years ago

0.23.1

3 years ago

0.23.0

3 years ago

0.22.1

3 years ago

0.22.0

3 years ago

0.21.0

4 years ago

0.21.1

4 years ago

0.20.1

4 years ago

0.20.0

4 years ago

0.19.1

4 years ago

0.19.0

4 years ago

0.18.3

4 years ago

0.18.2

4 years ago

0.18.1

4 years ago

0.15.0

4 years ago

0.16.0

4 years ago

0.17.0

4 years ago

0.18.0

4 years ago

0.14.0

4 years ago

0.13.0

4 years ago

0.12.0

4 years ago

0.11.0

4 years ago

0.10.0

4 years ago

0.9.0

4 years ago

0.8.0

4 years ago

0.7.0

4 years ago

0.6.0

4 years ago

0.5.1

4 years ago

0.5.0

4 years ago

0.4.0

4 years ago

0.3.0

4 years ago

0.2.0

4 years ago

0.1.0

4 years ago