Backend Development 10 min read

How APIJSON Lets You Build Full CRUD APIs with Just 3 Lines of Code

This article demonstrates how APIJSON dramatically reduces boilerplate for typical CRUD operations by using a single generic endpoint for all request types, showing concrete SpringBoot examples, request/response JSON structures, permission configuration, and advanced query capabilities, all with minimal code.

macrozheng
macrozheng
macrozheng
How APIJSON Lets You Build Full CRUD APIs with Just 3 Lines of Code

Many developers wonder how a simple SpringBoot interface can require three lines of code, while a full set of eight Hello‑World endpoints can balloon to dozens of lines, not to mention SQL, JDBC, ORM, and XML configuration. The traditional approach quickly becomes cumbersome.

Instead of writing separate URLs for each operation, you can use a single generic endpoint for each HTTP method:

<code>POST   base_url/post
DELETE (including batch)   base_url/delete
PUT    (including batch)   base_url/put
GET    (including list)    base_url/get
HEAD   (statistics)       base_url/head</code>

For example, to retrieve a single user you would call

base_url/get/

, and to retrieve a list you also call

base_url/get/

with appropriate query parameters. The same endpoint handles users, comments, and any other table.

APIJSON illustration
APIJSON illustration

APIJSON allows you to perform all CRUD operations on a table with just three lines of Java code. Below is a minimal user model with permission annotation:

<code>// Register the table and add default permissions
@MethodAccess
public class User {
    // Field descriptions for Android clients; server side can omit them
}

// Add permissions to the verifier
accessMap.put(User.class.getSimpleName(),
    getAccessMap(User.class.getAnnotation(MethodAccess.class)));
</code>

You can also customize POST permissions, for example allowing only UNKNOWN and ADMIN roles to create a User:

<code>@MethodAccess(
    POST = {UNKNOWN, ADMIN} // Only unauthenticated and admin roles can add User; default is {LOGIN, ADMIN}
)
public class User {}
</code>

After starting the server, you can query the API:

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

To fetch only female users and limit the returned fields:

<code>{
    "[]": {
        "User": {
            "sex": 1, // female
            "@column": "id,name" // only return id and name
        }
    }
}
</code>
<code>{
    "User[]": [
        {"id": 82002, "name": "Happy~"},
        {"id": 82003, "name": "Wechat"},
        {"id": 82005, "name": "Jan"}
    ],
    "code": 200,
    "msg": "success"
}
</code>

For more complex queries involving multiple tables, such as retrieving comments together with their authors:

<code>{
    "[]": {
        "Comment": {},
        "User": {
            "id@": "/Comment/userId"
        }
    }
}
</code>
<code>{
    "[]": [
        {
            "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"
}
</code>

APIJSON also supports pagination, ordering, grouping, aggregation, and custom functions via special keys such as

@order

,

@group

,

@having

, etc.

Write operations require permission configuration. When a request lacks permission, the server returns an error (see screenshot). After logging in, the role automatically becomes

LOGIN

(or a custom

@role

), and the request succeeds.

Permission error screenshot
Permission error screenshot
Successful request after login
Successful request after login

In summary, APIJSON eliminates the need to write individual CRUD endpoints. With only three lines of configuration code you obtain eight functional interfaces (add, delete, batch delete, update, batch update, query, pagination, statistics) and a rich set of query capabilities. For simple blogs or news sites that do not require permission control, you can even disable permission checks and avoid writing any code at all.

backend developmentSpringBootREST APICRUDPermission ManagementAPIJSON
macrozheng
Written by

macrozheng

Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.