yudaocode/SpringBoot-Labs · info · java.lang.IllegalArgumentException

id 参数不允许为空

Error message

id 参数不允许为空

What it means

Intentional validation exception in the Sentinel lab's file-storage variant. /annotations_demo (a @SentinelResource resource with blockHandler and fallback) throws IllegalArgumentException('id 参数不允许为空') when the optional id query parameter is omitted; the Sentinel annotation interceptor routes it to the fallback method, which returns 'fallback:id 参数不允许为空' over HTTP 200. The lab demonstrates fallback handling with rules persisted to a local file.

Source

Thrown at labx-04-spring-cloud-alibaba-sentinel/labx-04-sca-sentinel-file-provider/src/main/java/cn/iocoder/springcloudalibaba/labx04/sentineldemo/provider/controller/DemoController.java:69

            return "执行成功";
        } catch (BlockException ex) { // <3>
            return "被拒绝";
        } finally {
            // <4> 释放资源
            if (entry != null) {
                entry.exit();
            }
        }
    }

    // 测试「Sentinel @SentinelResource 注解」
    @GetMapping("/annotations_demo")
    @SentinelResource(value = "annotations_demo_resource",
            blockHandler = "blockHandler",
            fallback = "fallback")
    public String annotationsDemo(@RequestParam(required = false) Integer id) throws InterruptedException {
        if (id == null) {
            throw new IllegalArgumentException("id 参数不允许为空");
        }
        return "success...";
    }

    // BlockHandler 处理函数,参数最后多一个 BlockException,其余与原函数一致.
    public String blockHandler(Integer id, BlockException ex) {
        return "block:" + ex.getClass().getSimpleName();
    }

    // Fallback 处理函数,函数签名与原函数一致或加一个 Throwable 类型的参数.
    public String fallback(Integer id, Throwable throwable) {
        return "fallback:" + throwable.getMessage();
    }

}

View on GitHub (pinned to 6c12efaed0)

Solutions

  1. Add ?id=1 to see 'success...'.
  2. Use the fallback response as a sanity check that the annotation aspect and file-based rules are loaded.
  3. Make id a required parameter to get standard 400 validation instead.

Example fix

// before
public String annotationsDemo(@RequestParam(required = false) Integer id) {
    if (id == null) { throw new IllegalArgumentException("id 参数不允许为空"); }
    return "success...";
}

// after
public String annotationsDemo(@RequestParam Integer id) {
    return "success...";
}
Defensive patterns

Strategy: try-catch

Validate before calling

// Always call with an explicit id
// curl 'http://localhost:8080/demo/annotations_demo?id=1'

Try / catch

// Server already falls back; if wrapping without Sentinel:
try {
    return controller.annotationsDemo(id);
} catch (IllegalArgumentException e) {
    return "missing required parameter: id";
}

Prevention

When it happens

Trigger: GET /annotations_demo with no id parameter — the guard at line 69 throws and fallback echoes the message.

Common situations: Testing degrade/flow rules loaded from the sentinel-dashboard rules file and forgetting the id parameter; expecting an error status and getting the fallback body; mixing up which handler fires (fallback for exceptions, blockHandler for BlockException).

Related errors


AI-assisted analysis of yudaocode/SpringBoot-Labs@6c12efaed0 (2026-08-14). Data as JSON: /api/errors/9a3199645b649f90. Report an issue: GitHub.