後端開掛:3行代碼寫出8個介面!,後端3行

來源:互聯網
上載者:User

後端開掛:3行代碼寫出8個介面!,後端3行

肯定有不少人會想:

這怎麼可能呢?

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

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

8個沒啥用的Hello World介面就得24行代碼了!

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

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

 

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

擷取一個使用者:

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行就夠了:

//註冊表並添加許可權,用預設配置@MethodAccesspublic 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://39.108.143.172: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://39.108.143.172: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(Type0:value0,Type1:value1...)"   // 遠程調用函數"key@":"key0/key1.../targetKey"                    // 引用賦值"key$":"SQL搜尋運算式"                              // 模糊搜尋"key?":"Regex"                                 // 正則匹配"key+":key指定類型的Object                          // 增加/擴充"key-":key指定類型的Object                          // 減少/去除 "name:alias"                                      // 建立別名"@column":"id,sex,name"                           // 返回欄位"@group":"userId"                                 // 分組方式"@having":"max(id)>=100"                          // 彙總函式"@order":"date-,name+"                            // 排序方式

 

 

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

增:  http://39.108.143.172:8080/post

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

 

刪:  http://39.108.143.172:8080/delete

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

 

改:  http://39.108.143.172:8080/put

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

 

批量刪:  http://39.108.143.172:8080/delete

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

 

批量改:  http://39.108.143.172:8080/put

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

 

統計:  http://39.108.143.172:8080/head

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

 

 

  

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

事實上用APIJSON根本就不用自己寫介面!這3行代碼其實是為了做許可權管理!

像個人部落格、非商業的新聞資訊網站這種可以沒有許可權控制的,

改下全域配置,不做許可權校正,那就連一行代碼都不用寫了!!!

 

 

APIJSON - 後端超光速開發架構

 

Github源碼及文檔(右上方點Star支援下吧^_^)

https://github.com/TommyLemon/APIJSON

下載用戶端(測試伺服器位址:http://39.108.143.172:8080)

APIJSONClientApp.apk

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.