Now that we have four API swagger.yaml files available, let’s use light-codegen
to start four projects in light-example-4j/rest/swagger/ms_chain. In a normal API build,
you should create a repo for each API. For us, we have to user light-example-4j for all
the examples and tutorial for easy management in networknt github organization.
The light-codegen project is cloned to the local already during the prepare stage. Let’s
mvn clean install -DskipTests
Prepare Generator Config
Each generator in light-codegen requires several parameters to control how the generator
works. For more information on how to use generator, please refer light-codegen
For API A, here is the config.json and a copy can be found in the folder
model-config/rest/swagger/api_a/1.0.0 along with swagger.yaml and swagger.json.
As you can see the generated project will use 7001 for http and 7441 for https and client
module will be included as it will call API B with it. DB dependencies are not required for
Generate first project
Now you have your light-codegen built, let’s generate a project. Assume that model-config,
light-example-4j and light-codegen are in the same working directory ~/networknt and you are
in ~/networknt folder now.
You might realized that light-codegen is very fast compared to other code generators. It is
due to it using a rocker template engine which compiles templates into Java classes during the build
process. It supports dynamic template reloading but we are not using it. That is why is has
a info logging “INFO com.fizzed.rocker.runtime.RockerRuntime - Rocker template reloading not
activated”. This message can be simply ignored.
Build and run the mock API
And now you have a new project created in light-example-4j/rest/swagger/ms_chain/api_a/generated.
Let’s build it and run the test cases. If everything is OK, start the server.
mvn clean install exec:exec
Let’s test the API A by issuing the following command
By default the generated response example will be returned.
["Message 1","Message 2"]
As https port is enable, let’s test https connection and the result should be the same.
curl -k https://localhost:7441/v1/data
Generate other APIs
Let’s kill the API A by Ctrl+C and move to the workspace ~/networknt again. Follow the above steps
to generate other APIs. Make sure you are in ~/networknt directory.