alibaba/spring-ai-alibaba · error · IllegalArgumentException

数据项ID不能为空且必须大于0

Error message

数据项ID不能为空且必须大于0

What it means

Thrown by DatasetItemServiceImpl.deleteById() as an IllegalArgumentException when the id argument is null or <= 0. The service validates input before touching the database because MyBatis-Plus selectById/deleteById with a null id fails unclearly.

Solutions

  1. Ensure the caller passes a valid positive dataset item id
  2. Add @NotNull / @Positive bean-validation on the controller parameter
  3. Check the client request actually includes the id (no dropped path variable)
  4. Return a 400 response for invalid ids instead of letting it bubble as 500

Example fix

// before
Long id = extractId(request); // may be null
datasetItemService.deleteById(id);
// after
if (id == null || id <= 0) { throw new IllegalArgumentException("id must be a positive number"); }
datasetItemService.deleteById(id);
Defensive patterns

Strategy: validation

Validate before calling

if (id == null || id <= 0) { throw new IllegalArgumentException("id must be positive"); }

Type guard

boolean isValidItemId(Long id) { return id != null && id > 0; }

Try / catch

try { service.deleteById(id); } catch (IllegalArgumentException e) { return ResponseEntity.badRequest().body(e.getMessage()); }

Prevention

When it happens

Trigger: Calling deleteById(null), deleteById(0L), or deleteById with a negative id, typically from an unbound or default-initialized controller parameter.

Common situations: Missing path/query parameter mapped to a null Long; client sending 0 or garbage that parses to a non-positive value; uninitialized field defaults.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


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

Appendix: source

Thrown at spring-ai-alibaba-admin/spring-ai-alibaba-admin-server-start/src/main/java/com/alibaba/cloud/ai/studio/admin/service/impl/DatasetItemServiceImpl.java:162

        if(Objects.isNull(existingData)){
            log.warn("尝试更新不存在的数据项: {}", request.getId());
        }


        datasetItemMapper.update(request.getId(),request.getDataContent());

        return getById(request.getId());
    }

    @Override

    public void deleteById(Long id) {
        log.info("删除数据项: {}", id);
        
        // 数据验证
        if (id == null || id <= 0) {
            throw new IllegalArgumentException("数据项ID不能为空且必须大于0");
        }
        
        // 检查数据项是否存在
        DatasetItemDO existingItem = datasetItemMapper.selectById(id);
        if (existingItem == null) {
            log.warn("尝试删除不存在的数据项: {}", id);
            throw new RuntimeException("数据项不存在: " + id);
        }
        

        int result = datasetItemMapper.deleteById(id);
        if (result <= 0) {
            log.error("数据项删除失败: {}", id);
            throw new RuntimeException("数据项删除失败: " + id);
        }
        
        log.info("数据项删除成功: {}", id);
    }

View on GitHub (pinned to f82da0b50f)