然后在本地IDE中打开, light4j也有类似的在线生成的功能。下面笔者就带大家来一起了解一下。
在线生成light4j项目
light4j也有类似的在线生成功能,他的项目名称叫CodeGen,在线网址:https://codegen.lightapi.net/
模样如下:
功能菜单说:
1)Release
目前这里有1.6.x和2.0.x两个选项
Release是light4j的版本分支,目前只要是1.6.x和2.0.x版本,1.6.x目前使用JDK1.8开发,2.0.x是面向JDK11用户。
国内用户大多数用JDK1.8,此处选择1.6.x,如果你们公司的JDK版本比较新,而且想体验JDK11编程特性,当然也就选择2.0.x
2)Framework
和项目作者联系以后,得知该是一些API风格的框架,主要用于描述web接口的风格:目前有
OpenAPI
OpenAPI Kotlin
Swagger
GraqphQL
Hybrid
Swagger大家或多或少听说过,通过在类和方法注解方式导出API,方便测试。
如下图
笔者使用了OpenAPI,在网上找到一些一些文字:
What Is OpenAPI?
大意如下:
OpenAPI Sepcification(即以前的Swagger规范)是REST API的API描述格式。你可以在OpenAPI文件
中描述你的实体API,以及每个终端(/users)的操作(GET /users POST /users)的输入输出参数、身
份证验证方法、联系信息、许可、使用条款及其他信息。
API规范的可用YAML或者JSON来编写,学习成本较低,网站的OpenAPI规范可以在Github上获得。
(由于笔者没使用Swagger,各位看官可以根据自身情况决定是否去深入了解 框架。)
实践
笔者按照官方Steve Hu的codegen web视频教程将codeGen(点击此处可浏览youtube视频)的使用方法和大家分享一下。
如上图所示笔者选择1.6.x版本分支,因为本地使用JDK8开发,接口描述风格选择OpenAPI,
菜单功能说明
Model Option
填入暴露的接口API描述文件,如果是在线文件选择(Online URL),如果是本地编辑好的文本,选择复制粘贴(Copy/Paste)
Config Option
这个则是light4j独有的配置文件,类似于springboot的yaml/yml,properties一样的全局设置文件。
对应的测试用例地址可以从以下项目中获得,且看下图
以下以java经典的demo宠物商店(petstore)为例,相关配置在https://github.com/networknt/model-config/tree/master/rest获得
笔者选择openapi的framework,所以从openapi目录中寻找相关配置文件。
model option地址:https://raw.githubusercontent.com/networknt/model-config/master/rest/openapi/petstore/1.0.0/openapi.yaml
openapi: 3.0.0
info:
version: 1.0.0
title: Swagger Petstore
license:
name: MIT
servers:
- url: 'http://petstore.swagger.io/v1'
paths:
/pets:
get:
summary: List all pets
operationId: listPets
tags:
- pets
parameters:
- name: limit
in: query
description: How many items to return at one time (max 100)
required: false
schema:
type: integer
format: int32
security:
- petstore_auth:
- 'read:pets'
responses:
'200':
description: An paged array of pets
headers:
x-next:
description: A link to the next page of responses
schema:
type: string
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Pet'
example:
- id: 1
name: catten
tag: cat
- id: 2
name: doggy
tag: dog
default:
description: unexpected error
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
post:
summary: Create a pet
operationId: createPets
requestBody:
description: Pet to add to the store
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
tags:
- pets
security:
- petstore_auth:
- 'read:pets'
- 'write:pets'
responses:
'201':
description: Null response
default:
description: unexpected error
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'/pets/{petId}':
get:
summary: Info for a specific pet
operationId: showPetById
tags:
- pets
parameters:
- name: petId
in: path
required: true
description: The id of the pet to retrieve
schema:
type: string
security:
- petstore_auth:
- 'read:pets'
responses:
'200':
description: Expected response to a valid request
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
example:
id: 1
name: Jessica Right
tag: pet
default:
description: unexpected error
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
delete:
summary: Delete a specific pet
operationId: deletePetById
tags:
- pets
parameters:
- name: petId
in: path
required: true
description: The id of the pet to delete
schema:
type: string
- name: key
in: header
required: true
description: The key header
schema:
type: string
security:
- petstore_auth:
- 'write:pets'
responses:
'200':
description: Expected response to a valid request
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
examples:
response:
value:
id: 1
name: Jessica Right
tag: pet
default:
description: unexpected error
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
components:
securitySchemes:
petstore_auth:
type: oauth2
description: This API uses OAuth 2 with the client credential grant flow.
flows:
clientCredentials:
tokenUrl: 'https://localhost:6882/token'
scopes:
'write:pets': modify pets in your account
'read:pets': read your pets
schemas:
Pet:
type: object
required:
- id
- name
properties:
id:
type: integer
format: int64
name:
type: string
tag:
type: string
Error:
type: object
required:
- code
- message
properties:
code:
type: integer
format: int32
message:
type: string
Model URL地址:https://raw.githubusercontent.com/networknt/model-config/master/rest/openapi/petstore/1.0.0/config.json
{ "name": "petstore", "version": "3.0.1", "groupId": "com.networknt", "artifactId": "petstore", "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": false, "dockerOrganization": "networknt" }
经过以上设置然后点击生成(Generate)获得一个压缩包文件,本地打开如下
以上就是正式项目了,不过是一个用户案例(demo程序)。
打来自述文件README.MD
以上mvn命令是启动项目的
mvn clean install exec:exec
在终端(Terminal)中运行以上命令,大概可以看到以下运行结果
默认开启https服务,8443端口
我们再来回头看看哪个暴露api的描述文件。
本地找到handler.yml文
打开api接口申明的uri地址/specui.html
本地网址 https://localhost:8443/specui.html
至此大家应该明白了吧。这里暴露了一些rest风格的api接口地址。
我们还通过配置文件handler.yml找到了一些系统监控状况及诊断信息地址
服务器运行状态:
https://localhost:8443/server/info
服务器监控状况地址:https://localhost:8443/health/com.networknt.petstore-3.0.1
通过以上可以看出,这个框架笔者投入了很多心血和技术,满满的佩服。
程序在IDEA下面调试方式启动,
占用内存149.953125MB,通过工具我们可以看出,他的启动类是com.networknt.server.Server 通过解析配置文件来启动项目