用户工具

站点工具


light4j:codegen
git clone https://github.com/networknt/light-codegen.git
git checkout 1.6.38  //切换到最新稳定分支,light-hybrid-4j的templates的pom.xml缺少<version.fastscanner>2.18.1</version.fastscanner>,加上即可
mvn package -DskipTests -f light-codegen/pom.xml
git clone https://github.com/networknt/model-config.git
java -jar light-codegen/codegen-cli/target/codegen-cli.jar -f openapi -o petstore -m model-config/rest/openapi/petstore/1.0.0/openapi.json -c model-config/rest/openapi/petstore/1.0.0/config.json
cd petstore # 生成的项目为petstore
mvn package dependency:copy-dependencies -DskipTests -DoutputDirectory=target
mvn exec:exec # netstat -ano|findstr 8443,taskkill -pid {pid} -f
curl --insecure https://127.0.0.1:8443/v1/pets
git clone https://github.com/networknt/light-example-4j # 也可以从示例项目开始
tar zcvf light-example-4j.tgz --exclude=light-example-4j/.git light-example-4j/ # 打包示例
  • help
java -jar light-codegen/codegen-cli/target/codegen-cli.jar 
--config,-c:配置文件config.json,指定groupId、httpPort、supportDb、rootPackage等
--model,-m:模型文件,支持json、yaml格式;规范优先,提前设计好接口文档规范openapi.yml
--framework,-f:框架,openapi、swagger、openapi-spec、openapikotlin、light-graphql-4j等
--output,-o:输出目录(项目名,同config.json/artifactId),官方示例为-o light-example-4j/rest/openapi/petstore
  • model:以petstore示例

初始生成项目时需要熟悉OpenAPI规范,建议从light-example-4j添加需要的特性。

config.json
{
  "groupId": "com.networknt.example",
  "artifactId": "petstore",
  "version": "3.0.1",
  "rootPackage": "com.networknt.petstore",
  "handlerPackage":"com.networknt.petstore.handler",
  "modelPackage":"com.networknt.petstore.model",
  "overwriteHandler": true,
  "overwriteHandlerTest": true,
  "overwriteModel": true,
  "httpPort": 8080,
  "enableHttp": false,
  "httpsPort": 8443,
  "enableHttps": true,
  "enableHttp2": true,
  "enableRegistry": false,
  "supportDb": false,
  "supportH2ForTest": false,
  "supportClient": true,
  "dockerOrganization": "networknt"
}
openapi.yml,接口规范优先,代码生成、参数检查、权限控制等都根据openapi规范来执行
根据paths生成handler/{path}{method}Handler.java
根据components/schemas生成model/{schema}.java
  • framework:Generator.getFramework()
    • light-rest-4j:支持4种框架,openapi[kotlin|-spec] swagger,参考model配置:model-config/rest
    • light-graph-4j:支持1种框架,light-graphql-4j;参考model配置:model-config/graphql/starwars
    • light-hybrid-4j:支持2种框架,light-hybrid-4j-(server|service);参考model配置:model-config/hybrid/hello-world
  • hybrid:light-codegen的1.6.38分支,light-hybrid-4j下templates内的pom.xml缺少<version.fastscanner>2.18.1</version.fastscanner>
# 生成测试服务
java -jar light-codegen/codegen-cli/target/codegen-cli.jar -f light-hybrid-4j-server -c model-config/hybrid/hello-world/server/config.json -o hello-world/hello
java -jar light-codegen/codegen-cli/target/codegen-cli.jar -f light-hybrid-4j-service -c model-config/hybrid/hello-world/service1/config.json -m model-config/hybrid/hello-world/service1/schema.json -o hello-world/service1
java -jar light-codegen/codegen-cli/target/codegen-cli.jar -f light-hybrid-4j-service -c model-config/hybrid/hello-world/service2/config.json -m model-config/hybrid/hello-world/service2/schema.json -o hello-world/service2
#pom.xml,mvn eclipse:eclipse,vscode需要这样才可以正确识别java项目
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.networknt</groupId>
	<version>1.0.0</version>
	<artifactId>parent</artifactId>
	<packaging>pom</packaging>
	<modules>
		<module>hello</module>
		<module>service1</module>
		<module>service2</module>
	</modules>
</project>
# 构建,mvn package -DskipTests
mvn package -DskipTests -f hello/pom.xml
mvn package -DskipTests -f service1/pom.xml
mvn package -DskipTests -f service2/pom.xml
# 启动,launch.json
{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "java",
            "name": "hello",
            "request": "launch",
            "mainClass": "com.networknt.server.Server",
            "projectName": "hello",
            "cwd": "${workspaceFolder}",
            "vmArgs": "-cp hello/target/hello-1.0.0.jar:service1/target/service1-1.0.0.jar:service2/target/service2-1.0.0.jar"
        }
    ]
}
java -cp service1/target/service1-1.0.0.jar:service2/target/service2-1.0.0.jar -jar hello/target/hello-1.0.0.jar
# 测试
light4j/codegen.txt · 最后更改: 2022/04/14 14:07 由 admin