alibaba/spring-ai-alibaba · warning

解析itemIds字符串失败: {}

Error message

解析itemIds字符串失败: {}

What it means

CommonUtils.parseItemIds parses a JSON array string (e.g. "[1,2,3]") into a List<Long>; when JSONArray.parseArray or the Long conversion throws, it logs this warning and returns an empty ArrayList rather than propagating the exception. Callers silently receive an empty item id list for malformed input.

Source

Thrown at spring-ai-alibaba-admin/spring-ai-alibaba-admin-server-start/src/main/java/com/alibaba/cloud/ai/studio/admin/utils/CommonUtils.java:24

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
public class CommonUtils {

    public static List<Long> parseItemIds(String itemIds) {
        if (itemIds == null || itemIds.trim().isEmpty()) {
            return new ArrayList<>();
        }

        try {
            JSONArray jsonArray = JSONArray.parseArray(itemIds);
            return jsonArray.stream()
                    .map(obj -> Long.valueOf(obj.toString()))
                    .collect(Collectors.toList());
        } catch (Exception e) {
            log.warn("解析itemIds字符串失败: {}", itemIds, e);
            return new ArrayList<>();
        }
    }


    public static String extractRawText(String markdownCode) {
        // Find the start of a code block (3 or more backticks)
        int startIndex = -1;
        int delimiterLength = 0;

        for (int i = 0; i <= markdownCode.length() - 3; i++) {
            if (markdownCode.substring(i, i + 3).equals("```")) {
                startIndex = i;
                delimiterLength = 3;
                // Count additional backticks
                while (i + delimiterLength < markdownCode.length() && markdownCode.charAt(i + delimiterLength) == '`') {
                    delimiterLength++;
                }

View on GitHub (pinned to f82da0b50f)

Solutions

  1. Send/serialize the field as a proper JSON array of numeric ids, e.g. "[1,2,3]"
  2. Guard at the caller: validate the string is a non-empty JSON array before calling parseItemIds
  3. Handle the empty-list return explicitly — treat it as invalid input rather than 'no items' if ids are required
  4. Normalize legacy comma-separated data before parsing

Example fix

// before
List<Long> ids = CommonUtils.parseItemIds("1,2,3"); // returns []
// after
List<Long> ids = CommonUtils.parseItemIds("[1,2,3]");
if (ids.isEmpty() && StringUtils.isNotBlank(raw)) { throw new IllegalArgumentException("Invalid itemIds: " + raw); }
Defensive patterns

Strategy: validation

Validate before calling

if (StringUtils.isNotBlank(itemIdsStr) && itemIdsStr.trim().startsWith("[") && itemIdsStr.trim().endsWith("]")) { ids = CommonUtils.parseItemIds(itemIdsStr); }

Type guard

boolean isJsonArrayOfNumbers(String s) { if (s == null) return false; String t = s.trim(); return t.startsWith("[") && t.endsWith("]") && !t.equals("[]"); }

Prevention

When it happens

Trigger: Passing a string that is not a JSON array of numbers — e.g. null, empty string, plain text, "1,2,3" without brackets, or array elements that are non-numeric strings like "abc".

Common situations: Frontend sending an unserialized comma-separated string instead of JSON; form field left empty; legacy data stored in a different delimiter format; double-encoded JSON strings.

Understand the failure class

Background: JSON parse error: "Unexpected token" / "not valid JSON" / "failed to parse" — what JSON parsers are really complaining about — this error's family across 45 libraries.

Related errors


AI-assisted analysis of alibaba/spring-ai-alibaba@f82da0b50f (2026-09-09). Data as JSON: /api/errors/6f836e2b0594dc24. Report an issue: GitHub.