腾讯 APIJSON 5.1.0+ 的路由插件,可控地对公网暴露类 RESTful 简单接口,内部转成 APIJSON 格式请求来执行。
A router plugin for Tencent APIJSON 5.1.0+, expose undercontrolled RESTful-like HTTP API, map to APIJSON request and execute.
适合在公司外的公网可控地暴露 HTTP 接口,以及方便接入 ZooKeeper, Zuul, Apollo, Nacos, SpringSecurity, Shiro, Sentinel, Hystrix 等各种使用 URL 路由的 配置、鉴权、监控、限流、熔断 等网关/框架/组件。
HTTP APIs can be exposed to public network and be under control, and can easily integrate Configuration/Authentication/Monitoring/Limitation/Isolatation projects(gateway, framework, component) using URL routes like ZooKeeper, Zuul, Apollo, Nacos, SpringSecurity, Shiro, Sentinel, Hystrix.
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependency>
<groupId>com.github.APIJSON</groupId>
<artifactId>apijson-router</artifactId>
<version>LATEST</version>
</dependency>
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.APIJSON:apijson-router:latest'
}
@RestController
@RequestMapping("")
public class DemoController extends APIJSONRouterController<Long> {
}
2.Override router in DemoController, and add @PostMapping("router/{method}/{tag}") for router method
@PostMapping("router/{method}/{tag}")
@Override
public String router(@PathVariable String method, @PathVariable String tag, @RequestParam Map<String, String> params, @RequestBody String request, HttpSession session) {
return super.router(method, tag, params, request, session);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(DemoApplication.class, args);
APIJSONApplication.init();
APIJSONRouterApplication.init();
}
参考 APIJSONRouterController 的注释及 APIJSONBoot 的 DemoController 和 DemoApplication
See document in APIJSONRouterController and DemoController, DemoApplication in APIJSONBoot
Instead of step 1 and 2, you can use APIAuto to complete them automatically: click [+ Add], then click [Release simple API]
例如
E.g.
name: 查询动态列表
url: /router/get/momentList // 最后必须为 /{method}/{tag} 格式:method 必须为万能通用路由名;tag 不能为 Table 或 Table[] 格式
request:
{
"Moment[].page": 0, // 以 . 分割路径中的 key,映射以下 "Moment[]": { "page": 0 }
"Moment[].count": 10, // 以 . 分割路径中的 key,映射以下 "Moment[]": { "count": 10 }
"format": false // 映射以下 "format": false
}
apijson:
{
"Moment[]": {
"page": 0,
"count": 10,
"Moment": {
"@column": "id,userId,date"
}
},
"format": false
}
其它字段可不填,用默认值
Other columns can use default value
如果不需要校验参数则可跳过。
This step can be ignored if validation is not needed.
和普通的 APIJSON 格式请求基本一致,只是不会自动根据符合表名的 tag 来对 structure 包装一层 "Table": structure
The same as common APIJSON requests, but won't wrap structure with tag to "Table": structure
例如
E.g.
method: GET
tag: momentList
structure:
{
"MUST": "Moment[].page", // 必传 Moment[].page
"REFUSE": "!Moment[].count,!format,!", // 不禁传 Moment[].count 和 format,禁传 MUST 之外的其它所有 key
"TYPE": {
"format": "BOOLEAN", // format 类型必须是布尔 Boolean
"Moment[].page": "NUMBER", // Moment[].page 类型必须是整数 Integer
"Moment[].count": "NUMBER" // Moment[].count 类型必须是整数 Integer
}
}
启动项目后用 APIAuto/Postman 等 HTTP 接口测试工具发起请求
After run project and the server has started, you can use HTTP tools like APIAuto/Postman to send request
POST {base_url}/router/get/{tag} // tag 可为任意符合变量名格式的字符串
{
"showKey0": val0,
"showKey1.a1": val1,
"showKey2.a2.b2": val2
...
}
例如
E.g.
POST http://localhost:8080/router/get/momentList // 对应 Document 表配置的 url
{
"Moment[].page": 0,
"Moment[].count": 5,
"format": false
}
如果 parser.isNeedVerifyContent,则会经过 Request 表校验规则来校验,
If parser.isNeedVerifyContent, it will be validated with the rule in table Request
最后内部映射为:
Finally it will be mapped to:
{
"Moment[]": {
"page": 0,
"count": 5,
"Moment": {
"@order": "date-"
}
},
"format": false
}
执行完 APIJSON 格式的请求后返回对应结果
Server will execute APIJSON request and response
有问题可以去 Tencent/APIJSON 提 issue
Tencent/APIJSON#36