{"record":{"id":"1e8b4c9b06790bd4","repo":"yudaocode/SpringBoot-Labs","slug":"id-1e8b4c","errorCode":null,"errorMessage":"id 参数不允许为空","messagePattern":"id 参数不允许为空","errorType":"validation","errorClass":"java.lang.IllegalArgumentException","httpStatus":200,"severity":"info","filePath":"labx-04-spring-cloud-alibaba-sentinel/labx-04-sca-sentinel-nacos-provider/src/main/java/cn/iocoder/springcloudalibaba/labx04/sentineldemo/provider/controller/DemoController.java","lineNumber":69,"sourceCode":"            return \"执行成功\";\n        } catch (BlockException ex) { // <3>\n            return \"被拒绝\";\n        } finally {\n            // <4> 释放资源\n            if (entry != null) {\n                entry.exit();\n            }\n        }\n    }\n\n    // 测试「Sentinel @SentinelResource 注解」\n    @GetMapping(\"/annotations_demo\")\n    @SentinelResource(value = \"annotations_demo_resource\",\n            blockHandler = \"blockHandler\",\n            fallback = \"fallback\")\n    public String annotationsDemo(@RequestParam(required = false) Integer id) throws InterruptedException {\n        if (id == null) {\n            throw new IllegalArgumentException(\"id 参数不允许为空\");\n        }\n        return \"success...\";\n    }\n\n    // BlockHandler 处理函数，参数最后多一个 BlockException，其余与原函数一致.\n    public String blockHandler(Integer id, BlockException ex) {\n        return \"block：\" + ex.getClass().getSimpleName();\n    }\n\n    // Fallback 处理函数，函数签名与原函数一致或加一个 Throwable 类型的参数.\n    public String fallback(Integer id, Throwable throwable) {\n        return \"fallback：\" + throwable.getMessage();\n    }\n\n}\n","sourceCodeStart":51,"sourceCodeEnd":85,"githubUrl":"https://github.com/yudaocode/SpringBoot-Labs/blob/6c12efaed06d12907a0f40dd2ad1f7020aec8798/labx-04-spring-cloud-alibaba-sentinel/labx-04-sca-sentinel-nacos-provider/src/main/java/cn/iocoder/springcloudalibaba/labx04/sentineldemo/provider/controller/DemoController.java#L51-L85","documentation":"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.","triggerScenarios":"GET /annotations_demo without the id query parameter; the IllegalArgumentException is thrown at line 69 and handled by fallback.","commonSituations":"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.","solutions":["Request /annotations_demo?id=1 for the success path.","Interpret the fallback message as proof the Sentinel annotation aspect is active.","Declare id as a required @RequestParam to let Spring return 400 for omissions."],"exampleFix":"// before\npublic String annotationsDemo(@RequestParam(required = false) Integer id) {\n    if (id == null) { throw new IllegalArgumentException(\"id 参数不允许为空\"); }\n    return \"success...\";\n}\n\n// after\npublic String annotationsDemo(@RequestParam Integer id) {\n    return \"success...\";\n}","handlingStrategy":"try-catch","validationCode":"// GET /demo/annotations_demo?id=1 — no exception, returns 'success...'\nif (id != null) { /* proceed */ } else { /* ask caller for id */ }","typeGuard":null,"tryCatchPattern":"// Consumer of this endpoint:\nString body = restTemplate.getForObject(url + \"/annotations_demo?id={id}\", String.class, id);\nif (body != null && body.startsWith(\"fallback\")) {\n    handleBusinessFailure(body);\n}","preventionTips":["Always append ?id=<value>.","Check that Nacos-persisted rules loaded (dashboard shows the resource) so you know which handler fires.","Reserve fallback for business exceptions; use required params for protocol-level validation."],"tags":["sentinel","nacos","spring-cloud-alibaba","tutorial-demo","fallback","validation"],"backgroundTag":null,"analyzedSha":"6c12efaed06d12907a0f40dd2ad1f7020aec8798","analyzedAt":"2026-08-14T13:06:31.500Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}