Command Palette

Search for a command to run...

UnylyUnyly
Back to skills

api-design-safety

FreeNo executable scriptsNot checked

当设计或修改 REST API 响应结构、处理 API 返回值,或生成 Excel/CSV/PDF/对账文件等下游产物时触发。防止 API 设计缺陷导致的字段错位、类型歧义,以及生成产物时关键字段缺失但静默成功的问题。

docckerby doccker★ 1,061 GitHub stars

About this skill

API 设计安全规范

当设计或修改 REST API 响应结构时,防止常见的设计缺陷。


陷阱 #1: 泛型方法重载歧义

场景: 返回类型为 String 时,Java 重载解析可能匹配错误的方法

问题根因

Java 方法重载解析时,String 类型参数会优先匹配 success(String message) 而非 success(T data),导致数据进入错误的字段。

错误示例

// ApiResponse 有两个重载:
public static <T> ApiResponse<T> success(T data)
public static <T> ApiResponse<T> success(String message, T data)

// ❌ 错误: String 类型匹配到 success(String message)
String avatarUrl = "http://example.com/avatar.jpg";
return ApiResponse.success(avatarUrl);
// 结果: {"code":200, "message":"http://...", "data":null}
// 前端 data.data 拿到 null,导致功能异常

正确做法

// ✅ 方案1: 明确指定 message 参数(推荐)
return ApiResponse.success("上传成功", avatarUrl);
// 结果: {"code":200, "message":"上传成功", "data":"http://..."}

// ✅ 方案2: 使用泛型明确类型
return ApiResponse.<String>success(avatarUrl);

// ✅ 方案3: 包装为 DTO(复杂场景推荐)
return ApiResponse.success(new UploadResult(avatarUrl));

检查清单

  • 返回 String 类型时,是否明确指定 message 参数
  • 是否有单参数和双参数的重载方法
  • 前端是否正确解析 data 字段(而非 message)
  • 是否有单元测试验证响应结构

陷阱 #2: 响应字段语义不清

场景: message 和 data 字段职责混淆

规范

字段 用途 类型 示例
code 业务状态码 int 200, 400, 500
message 用户可读的提示信息 String "上传成功", "参数错误"
data 业务数据 T {"url": "..."}, [...]
timestamp 响应时间戳 String ISO 8601 格式

错误示例

// ❌ 错误: 把业务数据放在 message
return ApiResponse.success("avatars/2026-04/xxx.jpeg");

// ❌ 错误: message 包含技术细节
return ApiResponse.error("NullPointerException at line 42");

正确做法

// ✅ message 是用户提示,data 是业务数据
return ApiResponse.success("上传成功", avatarUrl);

// ✅ 错误信息对用户友好
return ApiResponse.error("文件格式不支持,请上传 JPG/PNG 格式");

陷阱 #3: 空值处理不一致

场景: 无数据时返回 null、{}、[] 不统一

规范

场景 推荐返回 说明
单个对象不存在 data: null 前端判断 if (!data)
列表为空 data: [] 前端可直接遍历
分页数据为空 data: {list: [], total: 0} 保持结构一致

错误示例

// ❌ 错误: 有时返回 null,有时返回空对象
if (user == null) {
    return ApiResponse.success(null);  // 不一致
}
return ApiResponse.success(new UserVO());

正确做法

// ✅ 统一返回 null 表示不存在
if (user == null) {
    return ApiResponse.success(null);
}
return ApiResponse.success(userVO);

// ✅ 列表统一返回空数组
List<UserVO> users = userService.list();
return ApiResponse.success(users);  // 永远不返回 null

陷阱 #4: HTTP 状态码与业务状态码混淆

场景: 业务失败时返回 HTTP 500

规范

场景 HTTP 状态码 业务 code 说明
成功 200 200 正常响应
参数错误 200 400 业务层校验失败
未授权 401 - 认证失败
无权限 403 - 授权失败
资源不存在 200 404 业务资源不存在
服务器错误 500 - 代码异常

错误示例

// ❌ 错误: 业务失败返回 HTTP 500
if (user == null) {
    throw new RuntimeException("用户不存在");  // HTTP 500
}

正确做法

// ✅ 业务失败返回 HTTP 200 + 业务 code
if (user == null) {
    return ApiResponse.error(404, "用户不存在");  // HTTP 200
}

// ✅ 只有代码异常才返回 HTTP 500
@ExceptionHandler(Exception.class)
public ResponseEntity<ApiResponse<?>> handleException(Exception e) {
    log.error("服务器错误", e);
    return ResponseEntity.status(500)
        .body(ApiResponse.error("服务器错误,请稍后重试"));
}

陷阱 #5: 生成下游产物前缺乏完整性校验(看似成功,下游静默失败)

场景: 导出 Excel/CSV/PDF、生成对账文件、推送第三方接口、批量通知等"产生下游消费产物"的功能,关键字段缺失时用空字符串/null/默认值兜底,文件能生成、接口能返回 200,但下游消费方(微信导入、对账系统、第三方平台)会静默失败或拒收。

问题根因

业务方关注"接口返回 200"和"文件生成成功",但真实成功的判断标准在下游消费方:

  • 微信发货模板要求 mchId 和 transactionId 必须存在,缺失会拒收
  • 对账文件要求每行有完整的 订单号 / 金额 / 时间,缺一项整批驳回
  • 物流推送要求 快递公司 和 快递单号 都存在,否则物流端报错

如果生成时缺值用 "" / null / 0 兜底,调用方会以为成功,等下游消费失败时才回头排查,浪费时间且影响业务。

错误示例

// ❌ 错误: 关键字段缺失时静默用空值兜底,Excel 能生成但微信导入失败
public byte[] export(Long tenantId, List<Long> orderIds) {
    String mchId = miniAppConfigRepository.findByTenantId(tenantId)
        .map(TenantMiniAppConfig::getMchId)
        .orElse("");
    Map<Long, PaymentRecord> paymentMap = loadPaymentMap(tenantId, orders);

    for (TradeOrder order : orders) {
        PaymentRecord payment = paymentMap.get(order.getId());
        String transactionId = payment != null ? payment.getTransactionId() : "";
        String company = packages.stream().map(ShippingPackage::getCompany)
            .filter(StringUtils::hasText).distinct().collect(joining(";"));
        // transactionId 为空仍会写入 Excel,微信发货模板按位置导入时会拒收
        writeRow(sheet, order, mchId, transactionId, company, ...);
    }
    return workbook.toByteArray();
}

正确做法

「必填字段一次画全 + 生成前预校验 + 缺失列出业务 ID + 整体失败」

public byte[] export(Long tenantId, List<Long> orderIds) {
    TenantMiniAppConfig config = miniAppConfigRepository.findByTenantId(tenantId)
        .orElseThrow(() -> new BusinessException("未配置小程序"));

    if (!StringUtils.hasText(config.getMchId())) {
        throw new BusinessException("小程序未配置微信支付商户号,无法导出");
    }

    Map<Long, PaymentRecord> paymentMap = loadPaymentMap(tenantId, orders);
    validateRequiredFields(orders, paymentMap);

    return buildWorkbook(orders, paymentMap, config.getMchI
Full SKILL.md on GitHub

Install api-design-safety in Claude Code & Claude Desktop

Sign up to install this skill

Create a free account to reveal the install command and save the skill to your library.

  • Reveal the one-line install command
  • Save skills to your synced library
  • Get notified when skills update
Sign up freeI already have an account

Allowed tools

Tools this skill is permitted to call.

No restriction — this skill can use any tool.

Bundled files

references/multi-lang-examples.md

FAQ

What does the api-design-safety skill do?

当设计或修改 REST API 响应结构、处理 API 返回值,或生成 Excel/CSV/PDF/对账文件等下游产物时触发。防止 API 设计缺陷导致的字段错位、类型歧义,以及生成产物时关键字段缺失但静默成功的问题。

How do I install the api-design-safety skill?

Copy the skill folder into ~/.claude/skills (the Claude Code tab above does this in one command), or install it as a plugin.

Does the api-design-safety skill run scripts?

No, this skill is instructions only (SKILL.md) with no executable scripts.

Related skills

Compare api-design-safety with