Skip to content

Latest commit

 

History

History
executable file
·
266 lines (203 loc) · 9.03 KB

README.md

File metadata and controls

executable file
·
266 lines (203 loc) · 9.03 KB

apijson-router

腾讯 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.

image

添加依赖

Add Dependency

Maven

1. 在 pom.xml 中添加 JitPack 仓库

1. Add the JitPack repository to pom.xml

	<repositories>
		<repository>
		    <id>jitpack.io</id>
		    <url>https://jitpack.io</url>
		</repository>
	</repositories>

image


2. 在 pom.xml 中添加 apijson-router 依赖

2. Add the apijson-router dependency to pom.xml

	<dependency>
	    <groupId>com.github.APIJSON</groupId>
	    <artifactId>apijson-router</artifactId>
	    <version>LATEST</version>
	</dependency>

image


https://github.com/APIJSON/APIJSON-Demo/blob/master/APIJSON-Java-Server/APIJSONBoot-MultiDataSource/pom.xml



Gradle

1. 在项目根目录 build.gradle 中最后添加 JitPack 仓库

1. Add the JitPack repository in your root build.gradle at the end of repositories

	allprojects {
		repositories {
			maven { url 'https://jitpack.io' }
		}
	}

2. 在项目某个 module 目录(例如 app) build.gradle 中添加 apijson-router 依赖

2. Add the apijson-router dependency in one of your modules(such as app)

	dependencies {
	        implementation 'com.github.APIJSON:apijson-router:latest'
	}


初始化

Initialization

1.新增一个 @RestController class DemoController extends APIJSONRouterController

1.Add a @RestController class DemoController extends APIJSONRouterController

@RestController
@RequestMapping("")
public class DemoController extends APIJSONRouterController<Long> { 
}

image


2.在 DemoController 重写 router 方法,加上注解 @PostMapping("router/{method}/{tag}")

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);
	}

image


3.在 DemoApplication.main 方法内,APIJSONAppication.init 后调用 APIJSONRouterApplication.init

3.In DemoApplication.main, call APIJSONRouterApplication.init after APIJSONAppication.init

	public static void main(String[] args) throws Exception {
		SpringApplication.run(DemoApplication.class, args);
		APIJSONApplication.init();
		APIJSONRouterApplication.init();
	}

image


参考 APIJSONRouterController 的注释及 APIJSONBootDemoControllerDemoApplication

See document in APIJSONRouterController and DemoController, DemoApplication in APIJSONBoot



使用

Usage

以下步骤 1, 2 可改为直接在 APIAuto 参数注入面板点击 [+ 添加] 按钮,再点击弹窗内 [发布简单接口] 按钮来自动完成

Instead of step 1 and 2, you can use APIAuto to complete them automatically: click [+ Add], then click [Release simple API]

image


1.在 Document 表配置请求映射

1.Add mapping rule in table Document

例如
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

image


2.在 Request 表配置校验规则

2.Add validation rule in table Request

如果不需要校验参数则可跳过。
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
    }
}

image


3.测试已配置的类 RESTful 简单接口

3.Test configured RESTful-like API

启动项目后用 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

image


有问题可以去 Tencent/APIJSON 提 issue
Tencent/APIJSON#36



点右上角 ⭐Star 支持一下,谢谢 ^_^

Please ⭐Star this project ^_^

https://github.com/APIJSON/apijson-router