Reducing CRUD Boilerplate with APIJSON: One Interface for All Operations
This article explains how APIJSON lets developers replace dozens of SpringBoot CRUD endpoints with a single, configurable interface for GET, POST, PUT, DELETE and HEAD requests, dramatically cutting code lines while supporting complex queries, pagination, joins and role‑based permissions.
Traditional SpringBoot development often requires three lines of code for the simplest endpoint and dozens of lines for a full set of CRUD interfaces, leading to excessive boilerplate when handling multiple tables and operations.
For example, a minimal SpringBoot controller method looks like:
@RequestMapping("test/{request}")
public String test(@PathVariable String request) {
return request + ": Hello World";
}Eight such "Hello World" endpoints would already consume 24 lines, not counting SQL, JDBC, ORM or XML configuration code.
APIJSON solves this problem by providing a single endpoint for each request type (GET, POST, PUT, DELETE, HEAD). With only three lines of Java code—registering the table and configuring permissions—developers can perform any CRUD operation.
// Register table and add default permissions
@MethodAccess
public class User {
// fields are optional for server side
}
// Add permissions to verifier
accessMap.put(User.class.getSimpleName(), getAccessMap(User.class.getAnnotation(MethodAccess.class)));After starting the server, requests are made to URLs such as http://apijson.cn:8080/get with JSON bodies that specify the target table and conditions. A simple query for a user looks like:
{
"User": {
"id": 82001
}
}The response returns the requested fields along with standard metadata:
{
"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"
}More complex queries can filter, select specific columns, rename fields, join multiple tables, paginate and sort, e.g., retrieving only female users with selected columns:
{
"[]": {
"User": {
"sex": 1,
"@column": "id,name"
}
}
}APIJSON also supports batch operations and statistical queries using the same endpoint pattern, with permission rules defined by annotations such as @MethodAccess(POST = {UNKNOWN, ADMIN}) . After logging in, the user's role automatically changes to LOGIN , satisfying the POST permission for Comment and allowing the operation.
In summary, by writing only three lines of configuration code, developers can eliminate the need to manually implement eight separate CRUD interfaces and gain a powerful, flexible JSON‑based API layer that handles arbitrary structures and relationships.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.