Implementation:APIJSON APIJSON Demo DemoController
| Knowledge Sources | |
|---|---|
| Domains | Web_Framework, API_Design, Routing |
| Last Updated | 2026-10-14 00:00 GMT |
Overview
Concrete tool for routing HTTP requests to generic CRUD operations provided by the APIJSON framework, implemented as a Spring @RestController.
Description
The DemoController class is the main REST entry point for the APIJSON Demo server. It extends APIJSONController<Long> and exposes generic POST endpoints (/get, /post, /put, /delete, /crud, /head, /gets, /heads) that delegate to the APIJSON framework's parser pipeline. Instead of per-resource endpoints, all tables are accessed through the same set of generic endpoints, with the target table and operations specified in the JSON request body.
The controller also provides tagged routes (/get/{tag}, /crud/{method}/{tag}) and a router endpoint (/router/{method}/{tag}) for simplified access patterns.
Usage
- Standard CRUD: Use
POST /getwith a JSON body specifying the table and conditions to query data. - Multi-operation: Use
POST /crudto perform create, read, update, and delete in a single request. - Tagged access: Use
POST /get/{tag}for simplified per-resource queries where tag is the table name. - Internal API calls: The static
CLIENTfield (RestTemplate) is used byAutoTestRegressionSchedulerfor HTTP calls to the Node.js test runner.
Code Reference
Source Location
- Repository: APIJSON-Demo
- File: APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/java/apijson/boot/DemoController.java
Signature
@Service
@RestController
@RequestMapping("")
public class DemoController extends APIJSONController<Long> {
// Generic CRUD endpoints
@PostMapping("crud")
public String crudAll(@RequestBody String request, HttpSession session);
@PostMapping("crud/{method}")
public String crud(@PathVariable("method") String method,
@RequestBody String request, HttpSession session);
@PostMapping("get")
public String get(@RequestBody String request, HttpSession session);
@PostMapping("head")
public String head(@RequestBody String request, HttpSession session);
@PostMapping("gets")
public String gets(@RequestBody String request, HttpSession session);
@PostMapping("heads")
public String heads(@RequestBody String request, HttpSession session);
@PostMapping("post")
public String post(@RequestBody String request, HttpSession session);
@PostMapping("put")
public String put(@RequestBody String request, HttpSession session);
@PostMapping("delete")
public String delete(@RequestBody String request, HttpSession session);
// Tagged routes
@PostMapping("router/{method}/{tag}")
public String router(@PathVariable("method") String method,
@PathVariable("tag") String tag,
@RequestParam Map<String, String> params,
@RequestBody String request, HttpSession session);
@PostMapping("crud/{method}/{tag}")
public String crudByTag(@PathVariable("method") String method,
@PathVariable("tag") String tag,
@RequestParam Map<String, String> params,
@RequestBody String request, HttpSession session);
@PostMapping("get/{tag}")
public String getByTag(@PathVariable("tag") String tag,
@RequestParam Map<String, String> params,
@RequestBody String request, HttpSession session);
// Static client for internal HTTP calls
public static RestTemplate CLIENT;
}
Import
import apijson.boot.DemoController;
// or via Spring autowiring
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| request | String | Yes | Raw JSON request body (APIJSON format) |
| session | HttpSession | Yes | User session for authentication and role |
| method | String | No | HTTP method name (for crud/router endpoints) |
| tag | String | No | Target table/resource name (for tagged routes) |
| params | Map<String, String> | No | Query parameters (for tagged routes) |
Outputs
| Name | Type | Description |
|---|---|---|
| return | String | JSON response string containing query results or operation status |
Usage Examples
Basic GET Query
# Query User table for user with id=82001
curl -X POST http://localhost:8080/get \
-H "Content-Type: application/json" \
-d '{"User":{"id":82001}}'
Multi-Operation CRUD
# Create a user and query their profile in one request
curl -X POST http://localhost:8080/crud \
-H "Content-Type: application/json" \
-d '{
"User": {
"id": 82002,
"name": "Test User",
"sex": 1
},
"@method": "POST",
"Privacy": {
"id": 82002,
"phone": "13000000002",
"_password": "123456"
},
"@gets": {
"tag": "User",
"User": { "id": 82002 }
}
}'
Tagged GET
# Simplified per-resource access
curl -X POST http://localhost:8080/get/User \
-H "Content-Type: application/json" \
-d '{"id": 82001}'
Related Pages
Implements Principle
- Principle:APIJSON_APIJSON_Demo_REST_API_Routing
- Principle:APIJSON_APIJSON_Demo_JSON_To_SQL_Translation