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

id 参数不允许为空

Error message

id 参数不允许为空

What it means

Intentional validation exception in the Sentinel lab's Nacos rule-storage variant. /annotations_demo is guarded by @SentinelResource(blockHandler, fallback); a missing optional id parameter triggers IllegalArgumentException('id 参数不允许为空'), which Sentinel intercepts and passes to the fallback method — the response becomes 'fallback:id 参数不允许为空' with HTTP 200. The point of this variant is Nacos-persisted Sentinel rules, but the fallback path is identical to the other modules.

Source

Thrown at labx-04-spring-cloud-alibaba-sentinel/labx-04-sca-sentinel-nacos-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. Request /annotations_demo?id=1 for the success path.
  2. Interpret the fallback message as proof the Sentinel annotation aspect is active.
  3. Declare id as a required @RequestParam to let Spring return 400 for omissions.

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 — no exception, returns 'success...'
if (id != null) { /* proceed */ } else { /* ask caller for id */ }

Try / catch

// Consumer of this endpoint:
String body = restTemplate.getForObject(url + "/annotations_demo?id={id}", String.class, id);
if (body != null && body.startsWith("fallback")) {
    handleBusinessFailure(body);
}

Prevention

When it happens

Trigger: GET /annotations_demo without the id query parameter; the IllegalArgumentException is thrown at line 69 and handled by fallback.

Common situations: Verifying that rules pulled from Nacos took effect and forgetting the parameter; expecting a 4xx/5xx and being confused by the 200 'fallback:...' body; curl tests omitting the query string entirely.

Related errors


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