yudaocode/SpringBoot-Labs · warning · IllegalArgumentException

id 参数不允许为空

Error message

id 参数不允许为空

What it means

Identical demo to error 8 but in lab-46-sentinel-demo-file, where Sentinel dashboard/rules are persisted via the file data source instead of Apollo. GET /demo/annotations_demo without id throws IllegalArgumentException, caught by the @SentinelResource fallback and returned as 'fallback: id 参数不允许为空'.

Source

Thrown at lab-46/lab-46-sentinel-demo-file/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. Pass ?id=1 for the success path.
  2. When fallback behavior seems stale, verify the file data source actually reloaded the rules (check Sentinel logs / dashboard connection) before blaming the fallback method.
  3. Keep fallback/blockHandler signatures exactly aligned with the resource method.
  4. Return a machine-readable degradation payload from fallback rather than a demo string for real services.

Example fix

// before: String fallback string only useful for demos
public String fallback(Integer id, Throwable throwable) {
    return "fallback:" + throwable.getMessage();
}

// after: structured degradation response
public CommonResult<String> fallback(Integer id, Throwable throwable) {
    return CommonResult.error(DEGRADED, "degraded: " + throwable.getMessage());
}
Defensive patterns

Strategy: validation

Validate before calling

if (id == null) { // in the caller/test
    throw new IllegalArgumentException("missing id"); // or just send ?id=1
}

Try / catch

// Inside the fallback, branch on cause type:
public String fallback(Integer id, Throwable t) {
    return (t instanceof IllegalArgumentException)
        ? "bad request: " + t.getMessage()
        : "degraded";
}

Prevention

When it happens

Trigger: GET /demo/annotations_demo with no id parameter on the file-based Sentinel lab; fallback method handles the thrown IllegalArgumentException.

Common situations: Same wiring pitfalls as error 8 (signature mismatch, missing fallbackClass). File-source specific: developers edit the rules JSON while the app runs and see stale behavior because the file source reloads only on change events (or needs a scheduled refresh), so fallback behavior may not match the file contents they just edited.

Related errors


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