LIGHT

  • News
  • Docs
  • Community
  • Reddit
  • GitHub

Generation

With the specification in place, we can generate the code with light-codegen

There are three different ways to generate the code with light-codegen:

  • Local build or download the released jar
  • Docker container
  • Script with docker container

To learn how to use the tool, please refer to the light-codegen tutorial

Generate code with a local build

Clone and build light-codegen

cd ~/networknt
git clone https://github.com/networknt/light-codegen.git
cd light-codegen
mvn clean install -DskipTests

For this demo, I am going to generate the code into light-example-4j/rest/openapi/database/generated folder so that users can check the code later on from this repository.

Let’s check out the light-example-4j repository and back up the existing database project. You can later compare your working folder with the .bak folder to see any difference.

cd ~/networknt
git clone https://github.com/networknt/light-example-4j.git
cd light-example-4j/rest/openapi
mv database database.bak

Before generating the project, we need to create a config.json to define packages, artifactId, groupId, and other options for the project.

Here is the content of the file and it can be found in ~/networknt/model-config/rest/openapi/database

{
  "name": "database",
  "version": "1.0.0",
  "groupId": "com.networknt",
  "artifactId": "database",
  "rootPackage": "com.networknt.database",
  "handlerPackage":"com.networknt.database.handler",
  "modelPackage":"com.networknt.database.model",
  "overwriteHandler": true,
  "overwriteHandlerTest": true,
  "overwriteModel": true,
  "httpPort": 8080,
  "enableHttp": false,
  "httpsPort": 8443,
  "enableHttps": true,
  "enableHttp2": true,
  "enableRegistry": false,
  "supportDb": true,
  "dbInfo": {
    "name": "mysql",
    "driverClassName": "com.mysql.jdbc.Driver",
    "jdbcUrl": "jdbc:mysql://mysqldb:3306/oauth2?useSSL=false",
    "username": "root",
    "password": "my-secret-pw"
  },
  "supportH2ForTest": true,
  "supportClient": false,
  "dockerOrganization": "networknt"
}

As you can see, we have enabled the Mysql database and H2 for test cases.

Code generation

cd ~/networknt
java -jar light-codegen/codegen-cli/target/codegen-cli.jar -f openapi -o light-example-4j/rest/openapi/database/generated -m model-config/rest/openapi/database/openapi.yaml -c model-config/rest/openapi/database/config.json

Now you should have a project generated. Let’s build it and run it.

cd ~/networknt/light-example-4j/rest/openapi/database/generated
mvn clean install exec:exec

Now you can access the service with curl following the [next step][] below.

Generate code with docker container

Let’s remove the generated folder from light-example-4j/rest/openapi/database folder and generate the project again with the docker container.

cd ~/networknt/light-example-4j/openapi/rest/database
rm -rf generated

Now let’s generate the project again with docker.

cd ~/networknt
docker run -it -v ~/networknt/model-config/rest/openapi/database:/light-api/input -v ~/networknt/light-example-4j/rest/openapi/database:/light-api/out networknt/light-codegen -f openapi -m /light-api/input/openapi.yaml -c /light-api/input/config.json -o /light-api/out/generated

Note that on Linux, the generated folder in ~/networknt/light-example-4j/rest/openapi/database will be owned by root:root and it needs to be changed to your user:group before compiling the project. On other operating systems like Mac and Windows, you don’t need this step.

cd ~/networknt/light-example-4j/rest/openapi/database
sudo chown -R steve:steve .

Let’s build and start the service

cd ~/networknt/light-example-4j/rest/openapi/database/generated
mvn clean install exec:exec

Now you can access the service with curl following the test step.

  • 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: August 29, 2019: update database tutorial (180f29f)
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