APIJSON/README.md
2016-12-07 23:29:40 +08:00

21 KiB

APIJSON

查看中文文档

A JSON Transmission Structure Protocal.

You can set any JSON structure and request your server, and the server will return a JSON String with the structure you had set like this:

Request:

{
    "[]": {                           //request an array
        "page": 1,                //array condition
        "count": 2,        
        "User": {                  //request an object from the table named User
            "sex": 0               //object condition
        },
        "Work": {
            "userId": “/User/id”  //rely path with default parent path,starts from the same level object's path
        },
        "Comment[]": {            //request an array named Comment
            "page": 0,
            "count": 3,
            "Comment": {
                 "workId": “[]/Work/id”  //full rely path
             }
        }
    }
}

Response:

{
    "[]":{
        "0":{
            "User":{
                "picture":"",
                "id":"38710",
                "sex":"0",
                "phone":"1300038710",
                "name":"Name-38710",
                "head":"http://www.tooopen.com/view/38710.html"
            },
            "Work":{
                "id":470,
                "title":"Title-470",
                "content":"This is a Content...-470",
                "userId":38710,
                "picture":"http://www.tooopen.com/view/470.html"
            },
            "Comment[]":{
                "0":{
                    "Comment":{
                        "id":4,
                        "parentId":0,
                        "workId":470,
                        "userId":310,
                        "targetUserId":14604,
                        "content":"This is a Content...-4",
                        "targetUserName":"targetUserName-14604",
                        "userName":"userName-93781"
                    }
                },
                "1":{
                    "Comment":{
                        "id":22,
                        "parentId":221,
                        "workId":470,
                        "userId":332,
                        "targetUserId":5904,
                        "content":"This is a Content...-22",
                        "targetUserName":"targetUserName-5904",
                        "userName":"userName-11679"
                    }
                },
                "2":{
                    "Comment":{
                        "id":47,
                        "parentId":4,
                        "workId":470,
                        "userId":10,
                        "targetUserId":5477,
                        "content":"This is a Content...-47",
                        "targetUserName":"targetUserName-5477",
                        "userName":"userName-80271"
                    }
                }
            }
        },
        "1":{
            "User":{
                "picture":"",
                "id":"70793",
                "sex":"0",
                "phone":"1300070793",
                "name":"Name-70793",
                "head":"http://www.tooopen.com/view/70793.html"
            },
            "Work":{
                "id":170,
                "title":"Title-73",
                "content":"This is a Content...-73",
                "userId":70793,
                "picture":"http://www.tooopen.com/view/73.html"
            },
            "Comment[]":{
                "0":{
                    "Comment":{
                        "id":44,
                        "parentId":0,
                        "workId":170,
                        "userId":7073,
                        "targetUserId":6378,
                        "content":"This is a Content...-44",
                        "targetUserName":"targetUserName-6378",
                        "userName":"userName-88645"
                    }
                },
                "1":{
                    "Comment":{
                        "id":54,
                        "parentId":0,
                        "workId":170,
                        "userId":3,
                        "targetUserId":62122,
                        "content":"This is a Content...-54",
                        "targetUserName":"targetUserName-62122",
                        "userName":"userName-82381"
                    }
                },
                "2":{
                    "Comment":{
                        "id":99,
                        "parentId":44,
                        "workId":170,
                        "userId":793,
                        "targetUserId":7166,
                        "content":"This is a Content...-99",
                        "targetUserName":"targetUserName-7166",
                        "userName":"userName-22949"
                    }
                }
            }
        }
    }
}

Compare with Previous HTTP Transmission Way

Process Previous way APIJSON
Transmission Server developers edit interfaces and update docs, then client developers request server and parse responses according to the docs Client developers request server and parse responses for their requirements. No inteface! No doc! No communication for any interface or doc between client and server developers!
Compatibility Server developers add new interfaces tagged with v2 and update docs Nothing need to do!
Client request Previous way APIJSON
Requirement Client developers append key-value pairs to an url for a request in docs Client developers append JSON to the url for their requirements
Structure base_url/lowercase_table_name?key0=value0&key1=value1...
&currentUserId=100&currentUserPassword=1234

The currentUserId and currentUserPassword is only for parts of interfaces
base_url/{TableName0:{key0:value0, key1:value1 ...}, TableName1:{...}...
, currentUserId:100, currentUserPassword:1234}

The currentUserId and currentUserPassword is only for parts of interfaces
URL Different urls for different methods(GET,POST...) or requirements One url for one method(GET,POST...)
Key-Value Pair key=value key:value
Server operation Previous way APIJSON
Parse and response Get key-value pairs and query the database with them by the default way, then encapsulate a JSON, finally return the JSON to client Just return what RequestParser#parse returned
Way of setting JSON structure to return Designed in server and cannot be modified by any client apps Designed by client apps and cannot be modified by sever
Client parse Previous way APIJSON
View structure Search docs or view logs after responses for requests Just view the requests, and viewing logs after responses for requests is also supported
Operate Parse JSON String from responses Parse with JSONResponse or use previous way
Client requests for different requirements Previous way APIJSON
User http://localhost:8080/user?id=1 http://localhost:8080/{"User":{"id":1}}
User and his Work Request twice
User: http://localhost:8080/user?id=1
Work: http://localhost:8080/work?userId=1
http://localhost:8080/{"User":{"id":1}, "Work":{"userId":"User/id"}}
User list http://localhost:8080/user/list?page=1&count=5&sex=0 http://localhost:8080/{"[]":{"page":1, "count":5, "User":{"sex":0}}}
Work list of which type is 1, each Work contains it's publisher User and top 3 Comments The Work must contains the User Object and Comment Array
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"}}}}
Work list of an User, each Work contains the publisher User and top 3 Comments Change type=1 to userId=1 above ①Change "Work":{"type":1}, "User":{"workId":"/Work/id"} to "User":{"id":1}, "Work":{"userId":"/User/id"} above

②Or save 4 repeated User by this way
http://localhost:8080/{"User":{"id":1}, "[]":{"page":1, "count":5, "Work":{"userId":"User/id"}, "[]":{"count":3, "Comment":{"workId":"[]/Work/id"}}}}

③If the User is already obtained, you can also save all repeated User by this way
http://localhost:8080/{"[]":{"page":1, "count":5, "Work":{"userId":1}, "[]":{"count":3, "Comment":{"workId":"[]/Work/id"}}}}
Server responses for different requests Previous way APIJSON
User {"status":200, "message":"success", "data":{"id":1, "name":"xxx"...}} {"status":200, "message":"success", "User":{"id":1, "name":"xxx"...}}
User and his Work Reponse twice
User: {"status":200, "message":"success", "data":{"id":1, "name":"xxx"...}}
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 list {"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...}}...}}
Work list of which type is 1, each Work contains it's publisher User and top 3 Comments {"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":{...}...}}
Work list of an User, each Work contains the publisher User and top 3 Comments {"status":200, "message":"success", "data":[{"id":1, "content":"xxx"..., "User":{...}, "Comment":[...]}, {"id":2...}...]} ①{"status":200, "message":"success", "[]":{"0":{"User":{"id":1, "name":"xxx"...}, "Work":{...}, "[]":{"0":{"Comment":{...}...}}}, "1":{...}...}}

②{"status":200, "message":"success", "User":{...}, "[]":{"0":{"Work":{"id":1, "content":"xxx"...}, "[]":{"0":{"Comment":{...}...}}}, "1":{...}...}}

③{"status":200, "message":"success", "[]":{"0":{"Work":{"id":1, "content":"xxx"...}, "[]":{"0":{"Comment":{...}...}}}, "1":{...}...}}

Usage

1.Download and unzip APIJSON project

Clone or download > Download ZIP > Unzip to a path and remember it.

2.Import MySQL table files

Start MySQLWorkbench > Enter a connection > Click Server menu > Data Import > Select the path of APIJSON-Master/table > Start Import > Refresh SCHEMAS, and you'll see the tables were already added.

3.Run Server project with Eclipse for JavaEE or IntellIJ IDEA Ultimate

If you haven't installed any editor above, please download and install one before run. JDK 1.8 is not supported yet, and 1.7 is suggested.
My config is Windows 7 + JDK 1.7.0_71 + Eclipse 4.6.1 + IntellIJ 2016.3 and OSX EI Capitan + JDK 1.7.0_71 + Eclipse 4.6.1 + IntellIJ 2016.2.5. The systems and softwares are all 64 bit.

Eclipse for JavaEE

1.Import
File > Import > Maven > Existing Maven Projects > Next > Browse > Select the path of APIJSON-Master/APIJSON(Server)/APIJSON(Eclipse_JEE) > Finish

2.Run
Run > Run As > Java Application > Select APIJSONApplication > OK

IntellIJ IDEA Ultimate

1.Import
Open > Select the path of APIJSON-Master/APIJSON(Server)/APIJSON(Idea) > OK

2.Run
Run > Run APIJSONApplication

4.Run Client project with ADT Bundle or Android Studio

If you haven't installed any editor above, please download and install one before run. You can skip this step and download the Client App below.
My config is Windows 7 + JDK 1.7.0_71 + ADT Bundle 20140702 + Android Studio 2.2 and OSX EI Capitan + JDK 1.7.0_71 + ADT Bundle 20140702 + Android Studio 2.1.2. The systems and softwares are all 64 bit.

ADT Bundle

1.Import
File > Import > Android > Existing Android Code Into Workspace > Next > Browse > Select the path of APIJSON-Master/APIJSON(Android)/APIJSON(ADT) > Finish

2.Run
Run > Run As > Android Application

Android Studio

1.Import
Open an existing Android Studio project > Select the path of APIJSON-Master/APIJSON(Android)/APIJSON(AndroidStudio) > OK

2.Run
Run > Run app

5.Operate Client app

Select an APIJSON request to send to server and wait. It will show the result received.
If the default url is not available, change it to an available one, such as an IPV4 address of a computer running APIJSON Server project. Then click the Query button to request again.

Download Client App

APIJSONClientApp.apk

If you have any questions or suggestions about APIJSON, you can send me an e-mail to tommylemon@qq.com.

Welcome star, welcome fork

https://github.com/TommyLemon/APIJSON

APIJSON, let interfaces go to hell !