yudaocode/SpringBoot-Labs · warning · IllegalArgumentException

id 参数不允许为空

Error message

id 参数不允许为空

What it means

Same @SentinelResource fallback demo in lab-46-sentinel-demo-nacos, which loads Sentinel rules from a Nacos config center. Omitting id on GET /demo/annotations_demo throws IllegalArgumentException and the fallback method converts it into the response 'fallback: id 参数不允许为空'.

Source

Thrown at lab-46/lab-46-sentinel-demo-nacos/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. Provide ?id=1 to exercise the normal path.
  2. Confirm Nacos connectivity (server-addr, namespace, group, dataId) when degradation rules look inactive; check startup logs for the Nacos datasource init result.
  3. Keep fallback signature: same params as resource method plus optional trailing Throwable.
  4. Handle the null id before Sentinel if you prefer Spring's 400 response over the fallback string.

Example fix

// before: fallback receives raw message
public String fallback(Integer id, Throwable throwable) {
    return "fallback:" + throwable.getMessage();
}

// after: differentiate business error from degradation in fallback
public String fallback(Integer id, Throwable throwable) {
    if (throwable instanceof IllegalArgumentException) {
        return "bad request: " + throwable.getMessage();
    }
    return "degraded";
}
Defensive patterns

Strategy: validation

Validate before calling

// Validate at the client before calling:
// if (!queryParams.containsKey("id")) throw ...;
// or server-side binding:
public String annotationsDemo(@RequestParam Integer id) { ... }

Try / catch

public String fallback(Integer id, Throwable throwable) {
    if (throwable instanceof IllegalArgumentException) {
        return "400: " + throwable.getMessage();
    }
    return "fallback: degraded";
}

Prevention

When it happens

Trigger: GET /demo/annotations_demo without an id parameter; requires the Nacos server reachable so rules (and the resource registration) load at startup.

Common situations: Nacos-specific failure mode: if the Nacos server is down or namespace/group mismatch, rules never load and developers misinterpret behavior — though for this particular error (parameter validation fallback) Sentinel's annotation fallback still works with default config. Signature/class wiring mistakes are the same as errors 8–9.

Related errors


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