2016-11-24 23:36:31 +08:00
# APIJSON
2016-11-25 01:07:32 +08:00
2017-01-26 12:03:09 +08:00
[English Document ](https://github.com/TommyLemon/APIJSON/blob/master/README(English ).md)
2016-11-26 17:39:55 +08:00
2017-01-26 12:00:03 +08:00
APIJSON是一种JSON传输结构协议。< br / >
2016-11-24 22:54:11 +08:00
2017-01-26 12:00:03 +08:00
客户端可以定义任何JSON结构去向服务端发起请求, 服务端就会返回对应结构的JSON字符串, 所求即所得。< br / >
一次请求任意结构任意数据,方便灵活,不需要专门接口或多次请求。< br / >
还能去除重复数据,节省流量提高速度!< br / >
2016-12-09 22:09:37 +08:00
2017-01-26 12:00:03 +08:00
从此HTTP传输JSON数据没有接口, 更不需要文档! < br / >
客户端再也不用和服务端沟通接口或文档问题了!再也不会被文档各种错误坑了!< br / >
服务端再也不用为了兼容旧版客户端写新版接口和文档了!再也不会被客户端随时随地没完没了地烦了!
2016-12-09 22:09:37 +08:00
2017-03-04 20:18:02 +08:00
举个栗子(查询类似微信朋友圈动态列表):
2016-11-24 23:22:03 +08:00
2017-01-26 12:00:03 +08:00
### 请求:
< p > {< br / >
2017-03-04 20:25:00 +08:00
" []" : { //请求一个array< br / >
2017-03-04 20:30:46 +08:00
" page" : 1, //array条件< br / >
2016-11-24 23:36:07 +08:00
" count" : 2, < br / >
2017-03-04 20:27:17 +08:00
" User" : { //请求查询名为User的table, 返回名为User的JSONObject< br / >
" sex" : 0 //object条件< br / >
2016-11-24 23:36:07 +08:00
},< br / >
2016-12-06 14:09:11 +08:00
" Work" : {< br / >
2017-03-04 20:32:39 +08:00
" userId@" : “ /User/id” //缺省依赖路径, 从同级object的路径开始< br / >
2016-11-25 00:21:39 +08:00
},< br / >
2017-03-04 20:33:18 +08:00
" Comment[]" : { //请求一个名为Comment的array < br / >
2016-11-24 23:36:07 +08:00
" page" : 0,< br / >
" count" : 3,< br / >
" Comment" : {< br / >
2017-03-04 20:33:18 +08:00
" workId@" : “ []/Work/id” //完整依赖路径< br / >
2016-11-24 23:36:07 +08:00
}< br / >
2016-11-25 00:21:39 +08:00
}< br / >
2016-11-24 23:36:07 +08:00
}< br / >
}< / p >
2016-11-24 23:22:03 +08:00
2017-01-26 12:00:03 +08:00
### 返回:
< p > {< br / >
2016-11-24 23:36:07 +08:00
" []" :{< br / >
" 0" :{< br / >
" User" :{< br / >
" picture" :" " ,< br / >
2017-02-11 20:27:21 +08:00
" id" :38710,< br / >
" sex" :0,< br / >
2016-11-24 23:36:07 +08:00
" phone" :" 1300038710" ,< br / >
" name" :" Name-38710" ,< br / >
2016-12-07 23:41:40 +08:00
" head" :" http://static.oschina.net/uploads/user/1218/2437072_100.jpg?t=1461076033000" < br / >
2016-11-24 23:36:07 +08:00
},< br / >
" Work" :{< br / >
" id" :470,< br / >
" title" :" Title-470" ,< br / >
" content" :" This is a Content...-470" ,< br / >
" userId" :38710,< br / >
2016-12-07 23:41:40 +08:00
" picture" :" http://static.oschina.net/uploads/user/585/1170143_50.jpg?t=1390226446000" < br / >
2016-11-24 23:36:07 +08:00
},< br / >
" Comment[]" :{< br / >
" 0" :{< br / >
" Comment" :{< br / >
" id" :4,< br / >
" parentId" :0,< br / >
" workId" :470,< br / >
" userId" :310,< br / >
" targetUserId" :14604,< br / >
" content" :" This is a Content...-4" ,< br / >
" targetUserName" :" targetUserName-14604" ,< br / >
" userName" :" userName-93781" < br / >
}< br / >
},< br / >
" 1" :{< br / >
" Comment" :{< br / >
" id" :22,< br / >
" parentId" :221,< br / >
" workId" :470,< br / >
" userId" :332,< br / >
" targetUserId" :5904,< br / >
" content" :" This is a Content...-22" ,< br / >
" targetUserName" :" targetUserName-5904" ,< br / >
" userName" :" userName-11679" < br / >
}< br / >
},< br / >
" 2" :{< br / >
" Comment" :{< br / >
" id" :47,< br / >
" parentId" :4,< br / >
" workId" :470,< br / >
" userId" :10,< br / >
" targetUserId" :5477,< br / >
" content" :" This is a Content...-47" ,< br / >
" targetUserName" :" targetUserName-5477" ,< br / >
" userName" :" userName-80271" < br / >
}< br / >
}< br / >
}< br / >
},< br / >
" 1" :{< br / >
" User" :{< br / >
" picture" :" " ,< br / >
2017-02-11 20:27:21 +08:00
" id" :70793,< br / >
" sex" :0,< br / >
2016-11-24 23:36:07 +08:00
" phone" :" 1300070793" ,< br / >
" name" :" Name-70793" ,< br / >
2016-12-07 23:41:40 +08:00
" head" :" http://static.oschina.net/uploads/user/1174/2348263_50.png?t=1439773471000" < br / >
2016-11-24 23:36:07 +08:00
},< br / >
" Work" :{< br / >
" id" :170,< br / >
" title" :" Title-73" ,< br / >
" content" :" This is a Content...-73" ,< br / >
" userId" :70793,< br / >
2016-12-07 23:41:40 +08:00
" picture" :" http://my.oschina.net/img/portrait.gif?t=1451961935000" < br / >
2016-11-24 23:36:07 +08:00
},< br / >
" Comment[]" :{< br / >
" 0" :{< br / >
" Comment" :{< br / >
" id" :44,< br / >
" parentId" :0,< br / >
" workId" :170,< br / >
" userId" :7073,< br / >
" targetUserId" :6378,< br / >
" content" :" This is a Content...-44" ,< br / >
" targetUserName" :" targetUserName-6378" ,< br / >
" userName" :" userName-88645" < br / >
}< br / >
},< br / >
" 1" :{< br / >
" Comment" :{< br / >
" id" :54,< br / >
" parentId" :0,< br / >
" workId" :170,< br / >
" userId" :3,< br / >
" targetUserId" :62122,< br / >
" content" :" This is a Content...-54" ,< br / >
" targetUserName" :" targetUserName-62122" ,< br / >
" userName" :" userName-82381" < br / >
}< br / >
},< br / >
" 2" :{< br / >
" Comment" :{< br / >
" id" :99,< br / >
" parentId" :44,< br / >
" workId" :170,< br / >
" userId" :793,< br / >
" targetUserId" :7166,< br / >
" content" :" This is a Content...-99" ,< br / >
" targetUserName" :" targetUserName-7166" ,< br / >
" userName" :" userName-22949" < br / >
}< br / >
}< br / >
}< br / >
}< br / >
}< br / >
}< / p >
2016-11-24 23:22:03 +08:00
2016-11-25 00:29:39 +08:00
2016-11-25 19:35:39 +08:00
2017-01-26 12:00:03 +08:00
![](https://github.com/TommyLemon/APIJSON/blob/master/picture/apijson_all_pages.jpg?raw=true)
![](https://github.com/TommyLemon/APIJSON/blob/master/picture/server_idea_log_complex.jpg)
![](https://github.com/TommyLemon/APIJSON/blob/master/picture/complex_json_cn.jpg?raw=true)
2017-03-04 19:59:01 +08:00
## 功能符
格式 | 功能 | 示例
---------- | ------------ | ----------
2017-03-04 20:11:15 +08:00
"key@":"依赖路径",依赖路径为用/分隔的字符串 | 表示依赖引用 | "userId@":"/User/id", userId依赖引用同级User内的id值, 假设id=1, 则请求完成后会变成"userId":1
2017-03-04 20:08:52 +08:00
"key$":"SQL搜索表达式", 任意标准SQL搜索表达式字符串, 如"%key%", "%k%e%y%"等 | 表示搜索查询 | "name$":"%Tommy%", 搜索包含Tommy的名字。一般用于查询一个数组。请求{"[]":{"User":{"name$":"%Tommy%"}}}会返回name包含"Tommy"的User数组
"key{}":JSONArray, JSONArray类似[object0,object1...]这种 | 表示匹配Array中任意一个 | "id{}":[38710,82001,70793], 查询id符合38710,82001,70793中任意一个的Object。一般用于查询一个数组。请求{"[]":{"User":{"id{}":[38710,82001,70793]}}}会返回id为38710,82001,70793其中任意一个的User数组。
2017-03-04 20:11:15 +08:00
"key()":"函数表达式", 函数表达式为 function(Type0:value0,Type1:value1...) | 表示远程调用函数 | "isPraised()":"contains(Collection:praiseUserIdList,userId)",请求完成会变为 "isPraised":true 这种( contains返回类型为boolean, 假设点赞用户id列表包含了userId, 即这个User点了赞)
2017-03-04 19:59:01 +08:00
2017-01-26 12:00:03 +08:00
## 对比传统HTTP传输方式
2016-12-06 20:31:52 +08:00
2017-01-26 12:00:03 +08:00
开发流程 | 传统方式 | APIJSON
2016-12-06 20:31:52 +08:00
-------- | ------------ | ------------
2017-01-26 12:00:03 +08:00
接口传输 | 等服务端编辑接口,然后更新文档,客户端再按照文档编辑请求和解析代码 | 客户端按照自己的需求编辑请求和解析代码。没有接口,更不需要文档!客户端再也不用和服务端沟通接口或文档问题了!
兼容旧版 | 服务端增加新接口, 用v2表示第2版接口, 然后更新文档 | 什么都不用做!
2016-12-06 20:31:52 +08:00
2017-01-26 12:00:03 +08:00
客户端请求 | 传统方式 | APIJSON
2016-12-06 20:31:52 +08:00
-------- | ------------ | ------------
2017-01-26 12:00:03 +08:00
要求 | 客户端按照文档在对应url后面拼接键值对 | 客户端按照自己的需求在固定url后拼接JSON
结构 | base_url/lowercase_table_name?key0=value0& key1=value1...< br / > & currentUserId=100& loginPassword=1234< br / > < br / > 其中currentUserId和loginPassword只在请求部分接口时需要 | base_url/{TableName0:{key0:value0, key1:value1 ...}, TableName1:{...}...< br / > , currentUserId:100, loginPassword:1234}< br / > < br / > 其中currentUserId和loginPassword只在请求部分接口时需要
URL | 不同的请求对应不同的url | 相同的请求方法(GET, POST等)都用同一个url
键值对 | key=value | key:value
2016-12-06 20:31:52 +08:00
2017-01-26 12:00:03 +08:00
服务端操作 | 传统方式 | APIJSON
2016-12-06 20:31:52 +08:00
-------- | ------------ | ------------
2017-01-26 12:00:03 +08:00
解析和返回 | 取出键值对, 把键值对作为条件用预设的的方式去查询数据库, 最后封装JSON并返回给客户端 | 把RequestParser#parse方法的返回值返回给客户端就行
返回JSON结构的设定方式 | 由服务端设定,客户端不能修改 | 由客户端设定,服务端不能修改
2016-12-06 20:31:52 +08:00
2017-01-26 12:00:03 +08:00
客户端解析 | 传统方式 | APIJSON
2016-12-06 20:31:52 +08:00
-------- | ------------ | ------------
2017-01-26 12:00:03 +08:00
查看方式 | 查文档或等请求成功后看log | 看请求就行, 所求即所得。也可以等请求成功后看log
方法 | 解析JSONObject | 可以用JSONResponse解析JSONObject或传统方式
2016-12-06 20:31:52 +08:00
2017-01-26 12:00:03 +08:00
客户端对应不同需求的请求 | 传统方式 | APIJSON
2016-12-06 20:31:52 +08:00
-------- | ------------ | ------------
User | http://localhost:8080/user?id=1 | http://localhost:8080/{"User":{"id":1}}
2017-01-26 12:00:03 +08:00
User和对应的Work | 分两次请求< br / > User: http://localhost:8080/user?id=1< br / > Work: http://localhost:8080/work?userId=1 | http://localhost:8080/{"User":{"id":1}, "Work":{"userId":"User/id"}}
User列表 | http://localhost:8080/user/list?page=1& count=5& sex=0 | http://localhost:8080/{"[]":{"page":1, "count":5, "User":{"sex":0}}}
type为1的Work列表, 每个Work包括发布者User和前3条Comment | Work里必须有User的Object和Comment的Array< br / > http://localhost:8080/work/list?page=1& count=5& type=1& commentCount=3 | http://localhost:8080/{"[]":{"page":1, "count":5, "Work":{"type":1}, "User":{"workId":"/Work/id"}, "[]":{"count":3, "Comment":{"workId":"[]/Work/id"}}}}
2017-01-31 11:35:29 +08:00
1个User发布的Work列表, 每个Work包括发布者User和前3条Comment | 把以上请求里的type=1改为userId=1 | 有以下几种方法:< br / > ①把以上请求里的"Work":{"type":1}, "User":{"workId":"/Work/id"}改为"User":{"id":1}, "Work":{"userId":"/User/id"}< br / > < br / > ②或这样省去4条重复User< br / > http://localhost:8080/{"User":{"id":1}, "[]":{"page":1, "count":5, "Work":{"userId":"User/id"}, "[]":{"count":3, "Comment":{"workId":"[]/Work/id"}}}}< br / > < br / > ③如果User之前已经获取到了, 还可以这样省去所有重复User< br / > http://localhost:8080/{"[]":{"page":1, "count":5, "Work":{"userId":1}, "[]":{"count":3, "Comment":{"workId":"[]/Work/id"}}}}
2016-12-06 20:31:52 +08:00
2017-01-26 12:00:03 +08:00
服务端对应不同请求的返回结果 | 传统方式 | APIJSON
2016-12-06 20:31:52 +08:00
-------- | ------------ | ------------
User | {"status":200, "message":"success", "data":{"id":1, "name":"xxx"...}} | {"status":200, "message":"success", "User":{"id":1, "name":"xxx"...}}
2017-01-26 12:00:03 +08:00
User和对应的Work | 分别返回两次请求的结果< br / > User: {"status":200, "message":"success", "data":{"id":1, "name":"xxx"...}}< br / > Work: {"status":200, "message":"success", "data":{"id":1, "name":"xxx"...}} | {"status":200, "message":"success", "User":{"id":1, "name":"xxx"...}, "Work":{"id":1, "content":"xxx"...}}
User列表 | {"status":200, "message":"success", "data":[{"id":1, "name":"xxx"...}, {"id":2...}...]} | {"status":200, "message":"success", "[]":{"0":{"User":{"id":1, "name":"xxx"...}}, "1":{"User":{"id":2...}}...}}
type为1的Work列表, 每个Work包括发布者User和前3条Comment | {"status":200, "message":"success", "data":[{"id":1, "content":"xxx"..., "User":{...}, "Comment":[...]}, {"id":2...}...]} | {"status":200, "message":"success", "[]":{"0":{"Work":{"id":1, "content":"xxx"...}, "User":{...}, "[]":{"0":{"Comment":{...}...}}}, "1":{...}...}}
2017-01-31 11:26:31 +08:00
1个User发布的Work列表, 每个Work包括发布者User和前3条Comment | {"status":200, "message":"success", "data":[{"id":1, "content":"xxx"..., "User":{...}, "Comment":[...]}, {"id":2...}...]} | 以上不同请求方法的结果:< br / > ①{"status":200, "message":"success", "[]":{"0":{"User":{"id":1, "name":"xxx"...}, "Work":{...}, "[]":{"0":{"Comment":{...}...}}}, "1":{...}...}}< br / > < br / > ②{"status":200, "message":"success", "User":{...}, "[]":{"0":{"Work":{"id":1, "content":"xxx"...}, "[]":{"0":{"Comment":{...}...}}}, "1":{...}...}}< br / > < br / > ③{"status":200, "message":"success", "[]":{"0":{"Work":{"id":1, "content":"xxx"...}, "[]":{"0":{"Comment":{...}...}}}, "1":{...}...}}
2016-12-06 20:31:52 +08:00
2017-01-26 12:00:03 +08:00
## 使用方法
2016-11-25 01:07:32 +08:00
2017-01-26 12:00:03 +08:00
### 1.下载后解压APIJSON工程
2016-11-25 01:07:32 +08:00
2017-01-26 12:00:03 +08:00
Clone or download > Download ZIP > 解压到一个路径并记住这个路径。
2016-11-25 01:07:32 +08:00
2017-01-26 12:00:03 +08:00
#### 你可以跳过步骤2和步骤3, 用我的服务器IP地址 139.196.140.118:8080 来测试服务端对客户端请求的返回结果。
2016-12-17 20:25:40 +08:00
2017-01-26 12:00:03 +08:00
### 2.导入MySQL table文件
2016-11-25 01:07:32 +08:00
2017-01-26 12:00:03 +08:00
服务端需要MySQL Server和MySQLWorkbench, 没有安装的都先下载安装一个。< br / >
我的配置是Windows 7 + MySQL Community Server 5.7.16 + MySQLWorkbench 6.3.7 和 OSX EI Capitan + MySQL Community Server 5.7.16 + MySQLWorkbench 6.3.8, 其中系统和软件都是64位的。
2016-12-09 22:34:22 +08:00
2017-01-26 12:00:03 +08:00
启动MySQLWorkbench > 进入一个Connection > 点击Server菜单 > Data Import > 选择刚才解压路径下的APIJSON-Master/table > Start Import > 刷新SCHEMAS, 左下方sys/tables会出现添加的table。
2016-11-25 01:07:32 +08:00
2017-01-26 12:00:03 +08:00
### 3.用Eclipse for JavaEE或IntellIJ IDEA Ultimate运行服务端工程
2016-11-25 01:07:32 +08:00
2017-01-26 15:41:57 +08:00
如果以上编辑器一个都没安装,运行前先下载安装一个。< br / >
2017-01-30 22:17:18 +08:00
我的配置是Windows 7 + JDK 1.7.0_71 + Eclipse 4.6.1 + IntellIJ 2016.3 和 OSX EI Capitan + JDK 1.8.0_91 + Eclipse 4.6.1 + IntellIJ 2016.2.5
2016-11-25 01:07:32 +08:00
2016-12-07 23:13:50 +08:00
#### Eclipse for JavaEE
2016-11-25 01:07:32 +08:00
2017-01-26 12:00:03 +08:00
1.导入< br / >
File > Import > Maven > Existing Maven Projects > Next > Browse > 选择刚才解压路径下的APIJSON-Master/APIJSON(Server)/APIJSON(Eclipse_JEE) > Finish
2016-12-07 23:13:50 +08:00
2017-01-26 12:00:03 +08:00
2.运行< br / >
Run > Run As > Java Application > 选择APIJSONApplication > OK
2016-12-07 23:13:50 +08:00
#### IntellIJ IDEA Ultimate
2017-01-26 12:00:03 +08:00
1.导入< br / >
Open > 选择刚才解压路径下的APIJSON-Master/APIJSON(Server)/APIJSON(Idea) > OK
2016-12-07 23:13:50 +08:00
2017-01-26 12:00:03 +08:00
2.运行< br / >
2016-12-07 23:13:50 +08:00
Run > Run APIJSONApplication
2017-01-26 12:00:03 +08:00
### 4.用ADT Bundle或Android Studio运行客户端工程
2016-12-07 23:13:50 +08:00
2017-01-26 12:00:03 +08:00
可以跳过这个步骤, 直接下载下方提供的客户端App。
2016-12-17 20:33:22 +08:00
2017-01-26 12:00:03 +08:00
如果以上编辑器一个都没安装,运行前先下载安装一个。< br / >
2017-01-30 22:17:18 +08:00
我的配置是Windows 7 + JDK 1.7.0_71 + ADT Bundle 20140702 + Android Studio 2.2 和 OSX EI Capitan +( JDK 1.7.0_71 + ADT Bundle 20140702) +( JDK 1.8.0_91 + Android Studio 2.1.2) , 其中系统和软件都是64位的。
2016-12-07 23:13:50 +08:00
#### ADT Bundle
2017-01-26 12:00:03 +08:00
1.导入< br / >
File > Import > Android > Existing Android Code Into Workspace > Next > Browse > 选择刚才解压路径下的APIJSON-Master/APIJSON(Android)/APIJSON(ADT) > Finish
2016-12-07 23:13:50 +08:00
2017-01-26 12:00:03 +08:00
2.运行< br / >
2016-12-07 23:13:50 +08:00
Run > Run As > Android Application
#### Android Studio
2017-01-26 12:00:03 +08:00
1.导入< br / >
Open an existing Android Studio project > 选择刚才解压路径下的APIJSON-Master/APIJSON(Android)/APIJSON(AndroidStudio) > OK
2016-12-07 23:13:50 +08:00
2017-01-26 12:00:03 +08:00
2.运行< br / >
2016-12-07 23:13:50 +08:00
Run > Run app
2016-11-25 01:07:32 +08:00
2017-01-26 12:00:03 +08:00
### 5.操作客户端App
2016-11-25 01:07:32 +08:00
2017-01-26 12:00:03 +08:00
选择发送APIJSON请求并等待显示结果。< br / >
如果默认url不可用, 修改为一个可用的, 比如正在运行APIJSON服务端工程的电脑的IPV4地址, 然后点击查询按钮重新请求。
2016-11-25 01:07:32 +08:00
2017-01-26 12:00:03 +08:00
## 下载试用客户端App
2016-11-25 00:29:39 +08:00
2016-11-25 01:20:17 +08:00
[APIJSONClientApp.apk ](http://files.cnblogs.com/files/tommylemon/APIJSON%28ADT%29.apk )
2016-11-25 19:51:19 +08:00
2016-11-26 13:09:01 +08:00
2017-01-26 12:00:03 +08:00
### 关于APIJSON如果你有任何问题或建议, 都可以发我邮件 tommylemon@qq.com.
2016-11-26 13:09:01 +08:00
2017-01-26 14:21:55 +08:00
## 更新日志
[https://github.com/TommyLemon/APIJSON/commits/master ](https://github.com/TommyLemon/APIJSON/commits/master )
2017-01-26 12:00:03 +08:00
## 欢迎Star, 欢迎Fork
2016-11-26 13:09:01 +08:00
2017-01-26 14:17:14 +08:00
[https://github.com/TommyLemon/APIJSON ](https://github.com/TommyLemon/APIJSON )
2017-01-26 12:00:03 +08:00
# APIJSON, 让接口见鬼去吧!