yudaocode/SpringBoot-Labs · warning · IllegalArgumentException

id 参数不允许为空

Error message

id 参数不允许为空

What it means

The baseline Sentinel lab (no external config center): GET /demo/annotations_demo without id throws IllegalArgumentException('id 参数不允许为空'), and @SentinelResource(fallback="fallback") routes it to the fallback method, returning 'fallback: id 参数不允许为空' with HTTP 200. Demonstrates Sentinel's exception-fallback mechanism in its simplest form.

Source

Thrown at lab-46/lab-46-sentinel-demo/src/main/java/cn/iocoder/springboot/lab46/sentineldemo/controller/DemoController.java:67

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

    // 测试 @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. Use ?id=1 for the success path.
  2. Remember: fallback = business exception path, blockHandler = Sentinel rule (BlockException) path; test them separately.
  3. When testing blocking, make the rule's resource name exactly 'annotations_demo_resource' and exceed the configured QPS.
  4. Fix parameter validation at the binding layer (required @RequestParam) if a 400 is preferred.

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: validation

Validate before calling

public String annotationsDemo(@RequestParam Integer id) {
    // Spring rejects a missing id with 400 before the method body runs
    return "success...";
}

Try / catch

// Optional: use fallback to separate validation from degradation
public String fallback(Integer id, Throwable throwable) {
    return throwable instanceof IllegalArgumentException
        ? "bad request"
        : "degraded";
}

Prevention

When it happens

Trigger: GET /demo/annotations_demo with no id on the plain sentinel-demo module. The blockHandler path triggers only under flow control (e.g., QPS rule on 'annotations_demo_resource'); this error is the fallback path.

Common situations: Canonical first-run Sentinel demo. Common confusion: developers see 'fallback: ...' and think a circuit breaker opened, when actually a plain business exception fired the fallback. Reverse confusion happens when they add a flow-control rule, hammer the endpoint, and still get the exception fallback because the resource name in the rule does not match value="annotations_demo_resource".

Related errors


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