yudaocode/SpringBoot-Labs · warning · IllegalArgumentException

id 参数不允许为空

Error message

id 参数不允许为空

What it means

In the Sentinel + Apollo lab, GET /demo/annotations_demo throws IllegalArgumentException('id 参数不允许为空') when the id request parameter is omitted. The method is annotated @SentinelResource(fallback="fallback"), so Sentinel routes the exception to the fallback(Integer id, Throwable) method, whose return value ('fallback: id 参数不允许为空') becomes the HTTP response instead of a 500. It demonstrates exception-based fallback (not flow-control blockHandler) in Sentinel.

Source

Thrown at lab-46/lab-46-sentinel-demo-apollo/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. Call the endpoint with an id: /demo/annotations_demo?id=1 — this is the intended successful path.
  2. If you want a 400 instead of the fallback string, validate the parameter earlier (e.g., @RequestParam(required=true)) or handle IllegalArgumentException in an @ControllerAdvice and exclude this resource from fallback.
  3. Keep the fallback method public, in the same class (or referenced via fallbackClass), with the exact same parameter list (+ optional Throwable).
  4. To distinguish degradation from business errors, configure blockHandler for BlockException and fallback for other Throwables as this demo does.

Example fix

// before: optional param + manual null check routed to Sentinel fallback
public String annotationsDemo(@RequestParam(required = false) Integer id) {
    if (id == null) {
        throw new IllegalArgumentException("id 参数不允许为空");
    }
    return "success...";
}

// after: let Spring enforce presence; no exception needed
public String annotationsDemo(@RequestParam Integer id) {
    return "success...";
}
Defensive patterns

Strategy: validation

Validate before calling

// Caller-side: always pass the parameter.
// GET /demo/annotations_demo?id=1
// Server-side: enforce presence at binding time:
public String annotationsDemo(@RequestParam Integer id) { ... }

Try / catch

// Not needed if fallback is configured; otherwise standard advice:
@ExceptionHandler(IllegalArgumentException.class)
CommonResult<?> badArg(IllegalArgumentException ex) {
    return CommonResult.error(INVALID_PARAM, ex.getMessage());
}

Prevention

When it happens

Trigger: GET /demo/annotations_demo without ?id=1 — @RequestParam(required=false) yields null id, the null check throws, and the fallback function's string is returned.

Common situations: Verifying Sentinel fallback wiring. Common real mistakes: fallback method signature not matching (must match params plus optional Throwable), fallback/blockHandler placed in a different bean without blockHandlerClass/fallbackClass, or expecting fallback to also catch BlockException — it does when only fallback is configured (Throwable param catches it), but blockHandler takes precedence when both are set and the cause is a flow-control block.

Related errors


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