一站式生成light4j工程初始化代码(基于petstore示例)
然后在本地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 Specification (formerly Swagger Specification) is an API description format for REST APIs. An OpenAPI file allows you to describe your entire API,
including:
- Available endpoints (
/users
) and operations on each endpoint (GET /users
,POST /users
)- Operation parameters Input and output for each operation
- Authentication methods
- Contact information, license, terms of use and other information.
API specifications can be written in YAML or JSON. The format is easy to learn and readable to both humans and machines. The complete OpenAPI Specification
can be found on GitHub: OpenAPI 3.0 Specification
大意如下:
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 通过解析配置文件来启动项目
原文地址:https://www.cnblogs.com/passedbylove/p/12092090.html
- 【自问自答】关于 Swift 的几个疑问
- 高级PHP应用程序漏洞审核技术【一】
- 【读书笔记】The Swift Programming Language (Swift 4.0.3)
- Ubuntu 下mysql常用操作
- JDK1.8源码(五)——java.util.ArrayList 类
- 代码审计入门总结
- ubuntu mysql启动|停止|重启
- JDK1.8源码(四)——java.util.Arrays 类
- JDK1.8源码(三)——java.lang.String 类
- JDK1.8源码(二)——java.lang.Integer 类
- Java关键字——instanceof
- 【强势推荐】一款可扫描CVE的工具
- Java的深拷贝和浅拷贝
- Java关键字——native
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- SpringBoot使用MySQL访问数据
- MySQL数据库与JDBC编程
- 自动删除QQ空间指定好友的留言
- 在Ubuntu 18.04中安装VMware工具
- 微信小程序下拉刷新功能
- 详解Linux Screen让程序保持后台运行
- Python Des加密与解密实现软件注册码、机器码
- Excel VBA 在保留原单元格数据的情况下,将计算的百分比加在后面
- 入门级别的面试题——LeetCode题目19:删除链表的倒数第N个节点
- python做web接口测试零散笔记--1
- 要一遍做对——LeetCode题目20:有效的括号
- 双指针算法练习(一)
- 一般是面试的热身题——LeetCode题目21:合并两个有序链表
- LeetCode题目22:括号生成
- OpenGL ES 3.0 | 着色器编译器