後端開掛:3行代碼寫出8個接口!

確定有很多人會想:javascript

這怎麼可能呢?java

就算用幾乎零配置的SpringBoot,寫一個最簡單的接口也得有3行代碼啊!git

@RequestMapping("test/{request}")
    public String test(@PathVariable String request) {
        return request + ": Hello World";
    }

8個沒啥用的Hello World接口就得24行代碼了!github

這還沒算 拼SQL連JDBC 或者 調用ORM庫 的代碼呢!json

更不用說還要寫 XML配置 的其它庫了!api

 

沒錯,用傳統方式就是這樣。數組

獲取一個用戶:緩存

base_url/get/user

獲取一個用戶列表:安全

base_url/get/user/list

獲取一個評論:服務器

base_url/get/comment

獲取一個評論列表:

base_url/get/comment/list

...

僅僅是查詢,一張表(對應客戶端的model)就要兩個接口了,

若是再加上增刪改,批量改批量刪,還有統計,那就得有8個接口了!

 

 

那麼我是怎麼解決的呢?

同一種類型的請求都只用一個接口:

增                  base_url/post

刪(包括批量)       base_url/delete

改(包括批量)       base_url/put

查(包括列表)       base_url/get

統計                base_url/head

 

用最經常使用的查詢請求舉例:

獲取一個用戶:

base_url/get/

獲取一個用戶列表:

base_url/get/

獲取一個評論:

base_url/get

獲取一個評論列表:

base_url/get

 ...

都是用同一個接口!

 

咱們用APIJSON來操做一張表,例如用戶表User,代碼寫3行就夠了:

//註冊表並添加權限,用默認配置
@MethodAccess
public class User {
//內容通常僅供表字段說明及Android App開發使用,服務端不用的可不寫。
}

//Verifier內添加權限
accessMap.put(User.class.getSimpleName(), getAccessMap(User.class.getAnnotation(MethodAccess.class)));

或者能夠再定製下POST請求的角色權限:

@MethodAccess(
  POST = {UNKNOWN, ADMIN} //只容許未登陸角色和管理員角色新增User,默認配置是 {LOGIN, ADMIN}
)
public class User {}

 

而後運行下Server工程就能夠請求了:

URL:http://apijson.cn:8080/get

表單:

{
    "User": {
        "id": 82001
    }
} 

返回:

{
    "User": {
        "id": 82001,
        "sex": 0,
        "name": "Test",
        "tag": "APIJSON User",
        "head": "http://static.oschina.net/uploads/user/19/39085_50.jpg",
        "contactIdList": [
            82004,
            82021,
            70793
        ],
        "pictureList": [
            "http://common.cnblogs.com/images/icon_weibo_24.png"
        ],
        "date": "2017-02-01 19:21:50.0"
    },
    "code": 200,
    "msg": "success"
}

 

 

上面只是查了一個User,若是咱們要查女性用戶列表,能夠這樣:

URL:http://apijson.cn:8080/get

表單:

{
    "[]": { //數組
        "User": {
            "sex": 1, //性別爲女
            "@column": "id,name" //只須要id,name這兩個字段
        }
    }
}

返回:

{
    "[]": [
        {
            "User": {
                "id": 82002,
                "name": "Happy~"
            }
        },
        {
            "User": {
                "id": 82003,
                "name": "Wechat"
            }
        },
        {
            "User": {
                "id": 82005,
                "name": "Jan"
            }
        }
    ],
    "code": 200,
    "msg": "success"
}

 

User被多包裹了一層?給數組命名爲 User[] 來去掉吧:

表單:

{
    "User[]": { //提取User
        "User": {
            "sex": 1, //性別爲女
            "@column": "id,name" //只須要id,name這兩個字段
        }
    }
}

返回:

{
    "User[]": [
        {
            "id": 82002,
            "name": "Happy~"
        },
        {
            "id": 82003,
            "name": "Wechat"
        },
        {
            "id": 82005,
            "name": "Jan"
        }
    ],
    "code": 200,
    "msg": "success"
}

 

還要進一步提取名字? User-name[] 知足你:

表單:

{
    "User-name[]": { //提取User.name
        "User": {
            "sex": 1, //性別爲女
            "@column": "name" //只須要name這個字段
        }
    }
}

返回:

{
    "User-name[]": [
        "Happy~",
        "Wechat",
        "Jan",
        "Meria",
        "Tommy"
    ],
    "code": 200,
    "msg": "success"
}

 

 

但若是是含多張表關聯的數組,就不要去掉了哦:

表單:

{
    "[]": {
        "Comment": {}, //評論
        "User": {      //發佈評論的用戶
            "id@": "/Comment/userId" //User.id = Comment.userId
        }
    }
}

返回:
 

{
    "[]": [
        {
            "Comment": {
                "id": 3,
                "toId": 0,
                "userId": 82002,
                "momentId": 15,
                "date": "2017-02-01 19:20:50.0",
                "content": "This is a Content...-3"
            },
            "User": {
                "id": 82002,
                "sex": 1,
                "name": "Happy~",
                "tag": "iOS",
                "head": "http://static.oschina.net/uploads/user/1174/2348263_50.png?t=1439773471000",
                "contactIdList": [
                    82005,
                    82001,
                    38710
                ],
                "pictureList": [],
                "date": "2017-02-01 19:21:50.0"
            }
        },
        {
            "Comment": {
                "id": 4,
                "toId": 0,
                "userId": 38710,
                "momentId": 470,
                "date": "2017-02-01 19:20:50.0",
                "content": "This is a Content...-4"
            },
            "User": {
                "id": 38710,
                "sex": 0,
                "name": "TommyLemon",
                "tag": "Android&Java",
                "head": "http://static.oschina.net/uploads/user/1218/2437072_100.jpg?t=1461076033000",
                "contactIdList": [
                    82003,
                    82005
                ],
                "pictureList": [
                    "http://static.oschina.net/uploads/user/1218/2437072_100.jpg?t=1461076033000",
                    "http://common.cnblogs.com/images/icon_weibo_24.png"
                ],
                "date": "2017-02-01 19:21:50.0"
            }
        }
    ],
    "code": 200,
    "msg": "success"
}

 

還有動態Moment和它的點贊用戶列表:

{
    "Moment": {},
    "User[]": {
        "User": {
            "id{}@": "Moment/praiseUserIdList" //id在點贊列表praiseUserIdList內
        }
    }
}

 

相似微信我的資料界面:

{
    "User": {},
    "Moment[]": { //朋友圈照片列表
        "Moment": {
            "@order":"date-", //按發佈時間date倒序排列
            "userId@": "User/id"
        }
    }
}

 

相似微信朋友圈的動態列表:

{
    "[]": {
        "count": 3, //只要3個
        "page": 2,  //要第2頁的
        "Moment": {},
        "User": {
            "id@": "/Moment/userId"
        },
        "Comment[]": {
            "Comment": {
                "momentId@": "[]/Moment/id"
            }
        }
    }
} 

...

 

任意結構,任意內容,任意組合,

想要什麼JSON結構、字段內容、表關聯組合查詢均可以徹底自定義!  

"key[]":{}                                         // 查詢數組

"key{}":[1,2,3]                                    // 匹配選項範圍

"key{}":"<=10,length(key)>1..."                    // 匹配條件範圍

"key()":"function(arg0,arg1...)"                   // 遠程調用函數

"key@":"key0/key1.../targetKey"                    // 引用賦值

"key$":"%abc%"                                     // 模糊搜索

"key?":"^[0-9]+$"                                  // 正則匹配

"key+":[1]                                         // 增長/擴展

"key-":888.88                                     // 減小/去除 

"name:alias"                                      // 新建別名

"@column":"id,sex,name"                           // 返回字段

"@group":"userId"                                 // 分組方式

"@having":"max(id)>=100"                          // 聚合函數

"@order":"date-,name+"                            // 排序方式

 

 

以上都是查詢請求,再試試 增刪改 和 統計 :

增:  http://apijson.cn:8080/post

{
    "Comment": {
        "userId": 82001,
        "momentId": 15,
        "content": "測試新增評論"
    },
    "tag": "Comment"
}

 

刪:  http://apijson.cn:8080/delete

{
    "Comment": {
        "id": 1510394480987
    },
    "tag": "Comment"
}

 

改:  http://apijson.cn:8080/put

{
    "Comment": {
        "id": 22,
        "content": "測試修改評論"
    },
    "tag": "Comment"
}

 

批量刪:  http://apijson.cn:8080/delete

{
    "Comment": {
        "id{}": [1510394480987, 1510394804925]
    },
    "tag": "Comment[]"
}

 

批量改:  http://apijson.cn:8080/put

{
    "Comment": {
        "id{}": [22, 114],
        "content": "測試批量修改評論"
    },
    "tag": "Comment[]"
}

 

統計:  http://apijson.cn:8080/head

{
    "Comment": {
        "content$": "%測試%" //內容包含 測試 兩個字
    }
}

 

寫操做須要對應的權限,就是用3行代碼配置的,請求報錯:

登陸后角色自動變爲LOGIN(可傳@role來自定義),符合Comment的POST權限配置,成功:

篇幅有限,安全機制、緩存、版本管理都有,之後會專門介紹。

  

 

回想下,代碼才寫了3行,就實現了包括增刪改查等各類操做的8個接口以及這麼多種查詢!

事實上用APIJSON根本就不用本身寫接口!這3行代碼實際上是爲了作權限管理!

像我的博客、非商業的新聞資訊網站這種能夠沒有權限控制的,

改下全局配置,不作權限校驗,那就連一行代碼都不用寫了!!!

 

 

APIJSON - 自動化接口和文檔

 

Github源碼及文檔(右上角點Star支持下吧^_^

https://github.com/TommyLemon/APIJSON

下載客戶端(測試服務器地址: http://apijson.cn:8080

APIJSONClientApp.apk

 

相關文章
相關標籤/搜索