⼀站式⽣成light4jlight-4j⼯程初始化代码(基于petstore⽰例)light4j介绍:
light4j也有类似的在线⽣成的功能。下⾯笔者就带⼤家来⼀起了解⼀下。
在线⽣成light4j项⽬
light4j也有类似的在线⽣成功能,他的项⽬名称叫CodeGen,在线⽹址:
模样如下:
功能菜单说:
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 Sepcification(即以前的Swagger规范)是REST API的API描述格式。你可以在OpenAPI⽂件
中描述你的实体API,以及每个终端(/users)的操作(GET /users POST /users)的输⼊输出参数、⾝
份证验证⽅法、联系信息、许可、使⽤条款及其他信息。
API规范的可⽤YAML或者JSON来编写,学习成本较低,⽹站的获得。
(由于笔者没使⽤Swagger,各位看官可以根据⾃⾝情况决定是否去深⼊了解框架。)
实践
笔者按照官⽅Steve Hu的codegen web视频教程将codeGen(点击此处可浏览)的使⽤⽅法和⼤家分享⼀下。
如上图所⽰笔者选择1.6.x版本分⽀,因为本地使⽤JDK8开发,接⼝描述风格选择OpenAPI,
菜单功能说明
Model Option
填⼊暴露的接⼝API描述⽂件,如果是在线⽂件选择(Online URL),如果是本地编辑好的⽂本,选择复制粘贴(Copy/Paste)
Config Option
这个则是light4j独有的配置⽂件,类似于springboot的yaml/yml,properties⼀样的全局设置⽂件。
对应的测试⽤例地址可以从以下项⽬中获得,且看下图
以下以java经典的demo宠物商店(petstore)为例,相关配置在获得
笔者选择openapi的framework,所以从openapi⽬录中寻相关配置⽂件。model option地址:
openapi: 3.0.0
info:
version: 1.0.0
title: Swagger Petstore
license:
name: MIT
servers:
- url: '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

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。