{"record":{"id":"cc7296dfbcd00e61","repo":"yudaocode/SpringBoot-Labs","slug":"id-cc7296","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-apollo-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-apollo-provider/src/main/java/cn/iocoder/springcloudalibaba/labx04/sentineldemo/provider/controller/DemoController.java#L51-L85","documentation":"Intentional validation exception in the Sentinel lab (Apollo rule-storage variant). /annotations_demo declares @SentinelResource with blockHandler and fallback; omitting the optional id parameter makes the method throw IllegalArgumentException('id 参数不允许为空'), which Sentinel intercepts and passes to the configured fallback — the client sees 'fallback：id 参数不允许为空' with HTTP 200. Identical demo code across the lab's storage variants (Apollo/Nacos/file/actuator).","triggerScenarios":"GET /annotations_demo with no id parameter; the fallback method receives the IllegalArgumentException as its Throwable argument and echoes its message.","commonSituations":"Exploring how Apollo-persisted Sentinel rules behave and hitting the fallback earlier than expected; testing whether blockHandler vs fallback fires — flow-control (BlockException) goes to blockHandler, this IllegalArgumentException goes to fallback; forgetting the query parameter in manual curl tests.","solutions":["Call /annotations_demo?id=1 for the success path.","Treat the 'fallback：...' response as the demo demonstrating fallback — no server fix needed.","If a hard 400 is wanted, make id a required @RequestParam."],"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 — parameter present, no exception\nif (id == null) { /* supply id before calling */ }","typeGuard":null,"tryCatchPattern":"// Server side this is already handled by the Sentinel fallback; on your own client:\ntry {\n    String r = call(\"/annotations_demo\", \"id\", \"1\");\n} catch (IllegalArgumentException e) {\n    // defensive: shouldn't happen when id is supplied\n}","preventionTips":["Include the id parameter in every request to this endpoint.","Keep fallback signatures matching (same params + Throwable) so routing works.","Don't treat the 200 'fallback：...' body as a server error."],"tags":["sentinel","apollo","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"}