LIGHT

  • News
  • Docs
  • Community
  • Reddit
  • GitHub

Generation

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.

Build light-codegen

The light-codegen project is cloned to the local already during the prepare stage. Let’s build it.

cd ~/networknt/light-codegen
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.

{
  "name": "apia",
  "version": "1.0.0",
  "groupId": "com.networknt",
  "artifactId": "apia",
  "rootPackage": "com.networknt.apia",
  "handlerPackage":"com.networknt.apia.handler",
  "modelPackage":"com.networknt.apia.model",
  "overwriteHandler": true,
  "overwriteHandlerTest": true,
  "overwriteModel": true,
  "httpPort": 7001,
  "enableHttp": true,
  "httpsPort": 7441,
  "enableHttps": true,
  "enableRegistry": false,
  "supportDb": false,
  "supportH2ForTest": false,
  "supportClient": true
}

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 this tutorial.

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.

cd ~/networknt
java -jar light-codegen/codegen-cli/target/codegen-cli.jar -f swagger -o light-example-4j/rest/swagger/ms_chain/api_a/generated -m model-config/rest/swagger/api_a/1.0.0/swagger.json -c model-config/rest/swagger/api_a/1.0.0/config.json

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.

cd ~/networknt/light-example-4j/rest/swagger/ms_chain/api_a/generated
mvn clean install exec:exec

Let’s test the API A by issuing the following command

curl localhost:7001/v1/data

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.

cd ~/networknt
java -jar light-codegen/codegen-cli/target/codegen-cli.jar -f swagger -o light-example-4j/rest/swagger/ms_chain/api_b/generated -m model-config/rest/swagger/api_b/1.0.0/swagger.json -c model-config/rest/swagger/api_b/1.0.0/config.json
java -jar light-codegen/codegen-cli/target/codegen-cli.jar -f swagger -o light-example-4j/rest/swagger/ms_chain/api_c/generated -m model-config/rest/swagger/api_c/1.0.0/swagger.json -c model-config/rest/swagger/api_c/1.0.0/config.json
java -jar light-codegen/codegen-cli/target/codegen-cli.jar -f swagger -o light-example-4j/rest/swagger/ms_chain/api_d/generated -m model-config/rest/swagger/api_d/1.0.0/swagger.json -c model-config/rest/swagger/api_d/1.0.0/config.json

Now you have four APIs generated from four OpenAPI specifications. Let’s check them in. Note that you might not have write access to this repo, so you can ignore this step.

cd ~/networknt/light-example-4j
git add .
git commit -m "checkin 4 apis"
git push origin master
  • About Light
    • Overview
    • Testimonials
    • What is Light
    • Features
    • Principles
    • Benefits
    • Roadmap
    • Community
    • Articles
    • Videos
    • License
    • Why Light Platform
  • Getting Started
    • Get Started Overview
    • Environment
    • Light Codegen Tool
    • Light Rest 4j
    • Light Tram 4j
    • Light Graphql 4j
    • Light Hybrid 4j
    • Light Eventuate 4j
    • Light Oauth2
    • Light Portal Service
    • Light Proxy Server
    • Light Router Server
    • Light Config Server
    • Light Saga 4j
    • Light Session 4j
    • Webserver
    • Websocket
    • Spring Boot Servlet
  • Architecture
    • Architecture Overview
    • API Category
    • API Gateway
    • Architecture Patterns
    • CQRS
    • Eco System
    • Event Sourcing
    • Fail Fast vs Fail Slow
    • Integration Patterns
    • JavaEE declining
    • Key Distribution
    • Microservices Architecture
    • Microservices Monitoring
    • Microservices Security
    • Microservices Traceability
    • Modular Monolith
    • Platform Ecosystem
    • Plugin Architecture
    • Scalability and Performance
    • Serverless
    • Service Collaboration
    • Service Mesh
    • SOA
    • Spring is bloated
    • Stages of API Adoption
    • Transaction Management
    • Microservices Cross-cutting Concerns Options
    • Service Mesh Plus
    • Service Discovery
  • Design
    • Design Overview
    • Design First vs Code First
    • Desgin Pattern
    • Service Evolution
    • Consumer Contract and Consumer Driven Contract
    • Handling Partial Failure
    • Idempotency
    • Server Life Cycle
    • Environment Segregation
    • Database
    • Decomposition Patterns
    • Http2
    • Test Driven
    • Multi-Tenancy
    • Why check token expiration
    • WebServices to Microservices
  • Cross-Cutting Concerns
    • Concerns Overview
  • API Styles
    • Light-4j for absolute performance
    • Style Overview
    • Distributed session on IMDG
    • Hybrid Serverless Modularized Monolithic
    • Kafka - Event Sourcing and CQRS
    • REST - Representational state transfer
    • Web Server with Light
    • Websocket with Light
    • Spring Boot Integration
    • Single Page Application
    • GraphQL - A query language for your API
    • Light IBM MQ
    • Light AWS Lambda
    • Chaos Monkey
  • Infrastructure Services
    • Service Overview
    • Light Proxy
    • Light Mesh
    • Light Router
    • Light Portal
    • Messaging Infrastructure
    • Centralized Logging
    • COVID-19
    • Light OAuth2
    • Metrics and Alerts
    • Config Server
    • Tokenization
    • Light Controller
  • Tool Chain
    • Tool Chain Overview
  • Utility Library
  • Service Consumer
    • Service Consumer
  • Development
    • Development Overview
  • Deployment
    • Deployment Overview
    • Frontend Backend
    • Linux Service
    • Windows Service
    • Install Eventuate on Windows
    • Secure API
    • Client vs light-router
    • Memory Limit
    • Deploy to Kubernetes
  • Benchmark
    • Benchmark Overview
  • Tutorial
    • Tutorial Overview
  • Troubleshooting
    • Troubleshoot
  • FAQ
    • FAQ Overview
  • Milestones
  • Contribute
    • Contribute to Light
    • Development
    • Documentation
    • Example
    • Tutorial
“Generation” was last updated: July 5, 2021: fixes #275 checked and corrected grammar/spelling for majority of pages (#276) (b3bbb7b)
Improve this page
  • News
  • Docs
  • Community
  • Reddit
  • GitHub
  • About Light
  • Getting Started
  • Architecture
  • Design
  • Cross-Cutting Concerns
  • API Styles
  • Infrastructure Services
  • Tool Chain
  • Utility Library
  • Service Consumer
  • Development
  • Deployment
  • Benchmark
  • Tutorial
  • Troubleshooting
  • FAQ
  • Milestones
  • Contribute