★★★★ Java | back end opening: write 8 interfaces in 3 lines of code

From: open source blog, author: lonely exploration

Link: https://my.oschina.net/tommylemon/blog/1574430

There must be a lot of people thinking: how is this possible? You're not bragging, are you?

Even with a spring boot with almost zero configuration, there must be 3 lines of code to write the simplest interface!

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

The eight useless Hello World interfaces need 24 lines of code!

This is not the code to spell SQL and JDBC or call ORM library!

Not to mention other libraries for XML configuration!

Yes, in the traditional way.

Get a user:

        base_url/get/user

Get a list of users:

        base_url/get/user/list

Get a comment:

        base_url/get/comment

Get a list of comments:

        base_url/get/comment/list

...

Just for query, a table (corresponding to the model of the client) needs two interfaces. If you add, delete and modify, batch change, batch delete, and statistics, there must be eight interfaces!

So how did I solve it?

All requests of the same type use only one interface:

increase base_url/post

Delete (including batch) base_url/delete

Modification (including batch) base_url/put

Search (including list) base_url/get

Statistics base_url/head

Take the most common query request as an example:

Get a user:

        base_url/get/

Get a list of users:

        base_url/get/

Get a comment:

        base_url/get

Get a list of comments:

        base_url/get

...

All use the same interface! How did I do it?

APIJSON, yes, that's it!

We use APIJSON to operate a table, such as the User table. Three lines of code are enough:

//Registry and add permissions, using the default configuration
@MethodAccess
public class User {
//The content is generally only used for table field description and Android App development. It is not required for the server.
}

//Verifier   Add permissions within
accessMap.put(User.class.getSimpleName(), getAccessMap(User.class.getAnnotation(MethodAccess.class)));

Or you can customize the role permissions requested by POST:

@MethodAccess(
  POST = {UNKNOWN, ADMIN} //Only unregistered roles and administrator roles are allowed to be added   User, the default configuration is   {LOGIN,   ADMIN}
)
public class User {}

Then run the following Server project to request:

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

Form:

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

return:

{
    "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"
}

Only one User is checked above. If we want to check the list of female users, we can do the following:

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

Form:

{
    "[]": { //array
        "User": {
            "sex": 1, //Gender is female
            "@column": "id,name" //Only the ID and name fields are required
        }
    }
}

return:

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

Is the User covered with an extra layer? Name the array User [] to remove it:

Form:

{
    "User[]": { //Extract User
        "User": {
            "sex": 1, //Gender is female
            "@column": "id,name" //Only the ID and name fields are required
        }
    }

return:

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

Do you want to extract the name further? User name [] satisfies you:

Form:

{
    "User-name[]": { //Extract User.name
        "User": {
            "sex": 1, //Gender is female
            "@column": "name" //Only the name field is required
        }
    }
}

return:

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

However, if it is an array with multiple table associations, do not remove it:

Form:

{
    "[]": {
        "Comment": {}, //comment
        "User": {      //Users who post comments
            "id@": "/Comment/userId" //User.id = Comment.userId
        }
    }
}

return:

{
    "[]": [
        {
            "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"
}

There are also dynamic Moment and its favorite user list:

{
    "Moment": {},
    "User[]": {
        "User": {
            "id{}@": "Moment/praiseUserIdList" //id is in the praise list praiseUserIdList
        }
    }
}

Similar to wechat profile interface:

{
    "User": {},
    "Moment[]": { //Circle of friends photo list
        "Moment": {
            "@order":"date-", //In reverse order of release date
            "userId@": "User/id"
        }
    }
}

Dynamic list similar to wechat circle of friends:

{
    "[]": {
        "count": 3, //Just three
        "page": 2,  //To page 2
        "Moment": {},
        "User": {
            "id@": "/Moment/userId"
        },
        "Comment[]": {
            "Comment": {
                "momentId@": "[]/Moment/id"
            }
        }
    }
} 

...

Any structure, any content, any combination,

You can completely customize the JSON structure, field content, table Association and combined query you want!

"key[]":{}                                         //  Query array

"key{}":[1,2,3]                                    //  Match option range

"key{}":"<=10,length(key)>1..."                    //  Matching condition range

"key()":"function(arg0,arg1...)"                   //  Remote call function

"key@":"key0/key1.../targetKey"                    //  Reference assignment

"key$":"%abc%"                                     //  Fuzzy search

"key?":"^[0-9]+$"                                  //  Regular matching

"key+":[1]                                         //  Add / expand

"key-":888.88                                     //  Reduction / removal  

"name:alias"                                      //  New alias

"@column":"id,sex,name"                           //  Return field

"@group":"userId"                                 //  Grouping mode

"@having":"max(id)>=100"                          //  Aggregate function

"@order":"date-,name+"                            //  sort order

The above are all query requests. Try adding, deleting, modifying and statistics again:

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

{
    "Comment": {
        "userId": 82001,
        "momentId": 15,
        "content": "Test new comments"
    },
    "tag": "Comment"
}

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

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

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

{
    "Comment": {
        "id": 22,
        "content": "Test modification comments"
    },
    "tag": "Comment"
}

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

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

Batch modification:   http://apijson.cn:8080/put

{
    "Comment": {
        "id{}": [22, 114],
        "content": "Test batch modification comments"
    },
    "tag": "Comment[]"
}

Statistics:   http://apijson.cn:8080/head

{
    "Comment": {
        "content$": "%test%" //Content contains   test   Two words
    }
}

The write operation requires the corresponding permission, which is configured with 3 lines of code. The request reports an error:

After logging in, the role will automatically change to login (which can be transferred to @ role for self definition), which conforms to the POST permission configuration of Comment. Success:

In retrospect, only three lines of code have been written to realize eight interfaces for various operations, including addition, deletion, modification and query, as well as so many queries!

In fact, you don't have to write your own interface with APIJSON! These three lines of code are actually for permission management!

Like personal blogs and non-commercial news and information websites, which can be controlled without permission,

Change the global configuration without permission verification, so you don't even need to write a line of code!!!

Keywords: Python Java Front-end

Added by amitvedak on Fri, 26 Nov 2021 05:32:07 +0200