Implementation:APIJSON APIJSON AbstractParser parseResponse
| Knowledge Sources | |
|---|---|
| Domains | ORM, Request_Processing, API_Design |
| Last Updated | 2026-10-11 00:00 GMT |
Overview
Concrete entry point for parsing declarative JSON API requests and assembling full JSON responses, provided by the APIJSON ORM framework.
Description
The parseResponse method on AbstractParser is the top-level orchestrator of the entire APIJSON request-to-response pipeline. It receives a raw JSON request (either as a String or a pre-parsed Map), extracts global keywords (@format, @database, @schema, @role, @method, @version, @tag), runs authentication and content verification, creates a verifier, begins a database transaction, recursively parses the request object tree via onObjectParse, commits or rolls back the transaction, and finally assembles and formats the response.
The method handles both query methods (GET/HEAD/GETS/HEADS) which skip transactions, and write methods (POST/PUT/DELETE) which require transaction management. On any exception during parsing, it rolls back the transaction and returns an error response with the appropriate code and message.
Usage
Import and use this method when you need to process an incoming APIJSON request and produce a JSON response. It is typically called by a servlet or controller layer after receiving an HTTP request. The parser instance must be configured with a requestMethod (from the URL path) and a visitor (authenticated user) before calling parseResponse.
Code Reference
Source Location
- Repository: APIJSON
- File: src/main/java/apijson/orm/AbstractParser.java
Signature
public M parseResponse(String request)
public M parseResponse(M request)
Type Parameters:
- T - visitor ID type (Long or String)
- M extends Map<String, Object> - request/response map type
- L extends List<Object> - list type for array results
Parameters (String overload):**
- request - Raw JSON request string from HTTP layer
Parameters (Map overload):**
- request - Pre-parsed JSON map (Map<String, Object>)
Returns: M - Response map containing code, msg, and query/write results
Import
import apijson.orm.AbstractParser;
// or via the Parser interface:
import apijson.orm.Parser;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| request | String or M (Map<String, Object>) | Yes | Raw JSON request string or pre-parsed map |
| requestMethod | RequestMethod | Yes | HTTP method (GET/POST/PUT/DELETE/CRUD), set on parser before calling |
| visitor | Visitor<T> | Yes | Authenticated user from session/token, set on parser before calling |
| isRoot | boolean | No | Whether this is the root response (default true) |
| globalFormat | Boolean | No | Extracted from @format directive in request |
| globalDatabase | String | No | Extracted from @database directive |
| globalRole | String | No | Extracted from @role directive |
Outputs
| Name | Type | Description |
|---|---|---|
| return | M (Map<String, Object>) | Response with code, msg, and table results nested under their keys |
| code | Integer | HTTP-style status code (200=success, 400=bad request, 401=not logged in, 403=forbidden, 404=not found, 500=server error) |
| msg | String | Status message |
| time | Long | Response timestamp (milliseconds) |
Usage Examples
Basic Request Processing
import apijson.orm.Parser;
import apijson.RequestMethod;
// In a servlet/controller:
Parser<Long, JSONObject, JSONArray> parser = APIJSONParser.newParser(RequestMethod.GET);
parser.setVisitor(visitor); // authenticated user from session
// Parse a GET request: { "User": { "id": 82001 } }
JSONObject response = parser.parseResponse("{\"User\":{\"id\":82001}}");
// Response: { "code": 200, "msg": "success", "User": { "id": 82001, "name": "Tom", ... } }
Write Request with Transaction
// POST request: { "Comment": { "userId": 82001, "momentId": 100, "content": "Hello" } }
Parser<Long, JSONObject, JSONArray> parser = APIJSONParser.newParser(RequestMethod.POST);
parser.setVisitor(visitor);
JSONObject response = parser.parseResponse(requestJson);
// Transaction is automatically begun, committed on success, rolled back on error
// Response: { "code": 200, "msg": "success", "Comment": { "id": 1, "userId": 82001, ... } }
Multi-Method Transaction (CRUD)
// CRUD request with per-object method overrides:
// { "Comment": { "content": "Updated" }, "id@": "/Comment/id", "@method": "PUT" }
Parser<Long, JSONObject, JSONArray> parser = APIJSONParser.newParser(RequestMethod.CRUD);
parser.setVisitor(visitor);
JSONObject response = parser.parseResponse(requestJson);
// Each table object can override the method via @method directive