Tencent/APIJSON · error · ServerException
批量新增/修改失败!{}/{}:{}
Error message
批量新增/修改失败!{}/{}:{} What it means
In a batch add/update loop, after executing each child request the parser requires code=200 AND count=1. If either fails and allowPartialFailed is not enabled, a ServerException is thrown carrying key/i and the child's msg (or a note that count != 1). This is deliberate: a 200 with count != 1 would silently mask partial writes.
Source
Thrown at APIJSONORM/src/main/java/apijson/orm/AbstractObjectParser.java:868
}
result = result == null ? null : JSON.get(result, childKey);
boolean success = JSONResponse.isSuccess(result);
int count = result == null ? 0 : getIntValue(result, JSONResponse.KEY_COUNT);
if (id == null && result != null) {
id = result.get(idKey);
}
if (success == false || count != 1) { //如果 code = 200 但 count != 1,不能算成功,掩盖了错误不好排查问题
if (allowPartialFailed) {
failedIds.add(id);
if (firstFailIndex < 0) {
firstFailIndex = i;
}
}
else {
throw new ServerException(
"批量新增/修改失败!" + key + "/" + i + ":" + (success ? "成功但 count != 1 !"
: (result == null ? "null" : getString(result, JSONResponse.KEY_MSG))
));
}
}
allCount += 1; // 加了 allowPartialFailed 后 count 可能为 0 allCount += count;
ids.add(id);
}
int failedCount = failedIds == null ? 0 : failedIds.size();
if (failedCount > 0 && failedCount >= allCount) {
throw new ServerException("批量新增/修改 " + key + ":[] 中 " + allCount + " 个子项全部失败!"
+ "第 " + firstFailIndex + " 项失败原因:" + (firstFailThrow == null ? "" : firstFailThrow.getMessage()));
}
M allResult = getParser().newSuccessResult();
if (failedCount > 0) {View on GitHub (pinned to 5284052872)
Solutions
- Read the embedded child msg in the exception — it identifies key/i and the real cause (constraint, verification, permission)
- Fix the offending row's data (unique fields, required columns, valid id) and resend the batch
- If some failures are acceptable, enable allowPartialFailed so the batch returns failedIdList instead of aborting
- For updates matching multiple rows, restrict with a unique primary key so count is exactly 1
Example fix
// before
{"User:[]":[{"id":1,"name":"a"},{"id":1,"name":"dup"}]} // duplicate id -> count!=1 or constraint error
// after
{"User:[]":[{"id":1,"name":"a"},{"id":2,"name":"ok"}]} Defensive patterns
Strategy: try-catch
Validate before calling
for (Object o : items) {
Map<?,?> m = (Map<?,?>) o;
if (m.get(idKey) == null) throw new IllegalStateException("missing " + idKey);
// check unique fields against local state before send
} Try / catch
catch (ServerException e) { if (e.getMessage().contains("批量新增/修改失败")) { String idx = extract(e.getMessage(), "(\d+)"); reportRow(idx, e.getMessage()); /* drop bad row, resend rest */ } else throw e; } Prevention
- Pre-validate unique/required fields per row client-side
- Send id on every update item so count is exactly 1
- Enable allowPartialFailed when partial success is acceptable and you handle failedIdList
When it happens
Trigger: Batch POST/PUT where a child row fails validation, triggers a DB constraint (duplicate key, FK violation), or the underlying exec returns count=0 or count>1 (e.g. an UPDATE matching multiple rows). allowPartialFailed is false (default), so the first failing child aborts everything.
Common situations: One row in a batch violates a UNIQUE constraint; STRUCTURE/REQUEST table verification rejects one child; an UPDATE without a unique id matching several rows; backend role/permission denying one child table; partial network/DB failure mid-batch.
Related errors
- 批量新增/修改 {}:{} 中 {} 个子项全部失败!第 {} 项失败原因:{}
- 批量新增/修改失败!{}/{}:value 中value不合法!类型必须是 OBJECT ,结构为 {} !
- {method}请求,{name}/{key} 里面的 {idInKey}:value 中value的类型只能是 [Lo
- {method}请求,{name}/{key} 里面的 {idInKey}:[] 中所有项都不能为 [ null, <=
- {method}请求,{name}/{key} 里面的 {idInKey}:[] 中所有项的类型都只能是 Long 或
AI-assisted analysis of Tencent/APIJSON@5284052872 (2026-08-14).
Data as JSON: /api/errors/1c9a811781784652.
Report an issue: GitHub.