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

id 参数不允许为空

Error message

id 参数不允许为空

What it means

Intentional validation exception in the Sentinel lab's base demo01 provider. The @SentinelResource-annotated /annotations_demo endpoint throws IllegalArgumentException when the optional id parameter is null; Sentinel's annotation aspect catches it and invokes the configured fallback, whose signature ends with a Throwable parameter. The endpoint therefore returns 'fallback:id 参数不允许为空' with HTTP 200 — the demo shows how fallback absorbs business exceptions.

Source

Thrown at labx-04-spring-cloud-alibaba-sentinel/labx-04-sca-sentinel-demo01-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. Append ?id=1 to the request to get 'success...'.
  2. Recognize 'fallback:id 参数不允许为空' as correct fallback behavior for this lab.
  3. Make the parameter required if you want missing values rejected as 400 by Spring MVC.

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

// GET /demo/annotations_demo?id=1 avoids the throw entirely
if (queryParams.containsKey("id")) { /* safe to call */ }

Try / catch

// In your own non-Sentinel wrapper around this endpoint:
try {
    result = client.get("/annotations_demo?id=" + id);
} catch (HttpStatusCodeException e) {
    log.warn("annotations_demo failed: {}", e.getResponseBodyAsString());
}

Prevention

When it happens

Trigger: GET /demo/annotations_demo without ?id=... — id binds to null, the guard throws, fallback returns the message.

Common situations: First run of the demo without reading the tutorial's expected curl command; verifying Sentinel dependencies (sentinel-annotation-aspect bean) and using this response as evidence the aspect is active; mistaking the fallback body for an error status.

Related errors


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