在Feign 调用中模拟 Swagger 请求本质是构建一个与 Swagger 请求体完全一致的 JSON 字符串然后塞入 DTO 的data字段类型为Object。下面用Gson和Hutool两种方式给出完整代码示例。一、Swagger 中的 JSON 请求体示例假设你在 Swagger 中提交的 JSON 是json{ phone: 15921892280, templateCode: SMS_123456789, templateParam: { code: 1234, extra: { param: test } } }注意templateParam是一个嵌套 JSON 对象不是字符串。二、使用 Gson 构建相同的 JSON1. 构建嵌套的JsonObjectjavaimport com.google.gson.JsonObject; // 构建内层 extra JsonObject extra new JsonObject(); extra.addProperty(param, test); // 构建 templateParam JsonObject templateParam new JsonObject(); templateParam.addProperty(code, 1234); templateParam.add(extra, extra); // 嵌套对象 // 构建根对象 JsonObject root new JsonObject(); root.addProperty(phone, 15921892280); root.addProperty(templateCode, SMS_123456789); root.add(templateParam, templateParam);2. 转为 JSON 字符串javaString jsonString root.toString(); System.out.println(jsonString); // 输出: {phone:15921892280,templateCode:SMS_123456789,templateParam:{code:1234,extra:{param:test}}}3. 设置到 DTO 并调用 Feignjava// DTO 中的 data 字段是 Object MessageSendReq req new MessageSendReq(); req.setData(jsonString); // 传入 JSON 字符串 // Feign 调用 messageClient.sendMessage(req);三、如果 Swagger 中的templateParam是字符串而不是对象有些接口要求templateParam是JSON 字符串如阿里云短信此时 Swagger 请求体可能是json{ phone: 15921892280, templateCode: SMS_123456789, templateParam: {\code\:\1234\,\extra\:{\param\:\test\}} }这种情况下你需要先构建内部 JSON再转为字符串作为值java// 1. 构建内部对象 JsonObject inner new JsonObject(); inner.addProperty(code, 1234); JsonObject extra new JsonObject(); extra.addProperty(param, test); inner.add(extra, extra); // 2. 将内部对象转为 JSON 字符串 String innerJson inner.toString(); // {code:1234,extra:{param:test}} // 3. 构建根对象 JsonObject root new JsonObject(); root.addProperty(phone, 15921892280); root.addProperty(templateCode, SMS_123456789); root.addProperty(templateParam, innerJson); // 注意作为字符串传入 String finalJson root.toString();四、使用 Hutool更简洁推荐如果项目已引入 Hutool可以更直观地构建javaimport cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; // 构建内层 JSONObject extra JSONUtil.createObj() .put(param, test); JSONObject templateParam JSONUtil.createObj() .put(code, 1234) .put(extra, extra); // 构建根对象 JSONObject root JSONUtil.createObj() .put(phone, 15921892280) .put(templateCode, SMS_123456789) .put(templateParam, templateParam); // 转为字符串 String jsonString root.toString(); // 设置到 DTO req.setData(jsonString);五、JsonObject和String的区别关键点传入 DTO 的类型Jackson 序列化后的 HTTP BodySwagger 是否一致JsonObject❌ 序列化失败抛出异常—MapString, Object{data:{code:1234}}✅ 如果 Swagger 是对象则一致StringJSON 字符串{data:{\code\:\1234\}}⚠️ 如果 Swagger 是对象则多了引号结论你必须明确 Swagger 中data字段期望的是JSON 对象还是JSON 字符串。如果期望JSON 对象你应该传入Map或 POJO而不是String。如果期望JSON 字符串如阿里云短信则传入String。六、如何确认 Swagger 期望的类型查看 Swagger 的请求体示例如果显示为{ code: 1234 }→ 是JSON 对象如果显示为{\code\:\1234\}→ 是JSON 字符串根据你的情况阿里云短信的TemplateParam是字符串所以用jsonObject.toString()是正确的。七、完整调用示例Gson 版javapublic void sendSms() { // 1. 构建参数 JsonObject param new JsonObject(); param.addProperty(code, 1234); param.addProperty(phone, 15921892280); // 如果有嵌套 JsonObject extra new JsonObject(); extra.addProperty(type, notification); param.add(extra, extra); // 2. 转为 JSON 字符串因为阿里云要求 TemplateParam 是字符串 String templateParamStr param.toString(); // 3. 构建请求 DTO MessageSendReq req new MessageSendReq(); req.setPhone(15921892280); req.setTemplateCode(SMS_123456789); req.setData(templateParamStr); // 传入字符串 // 4. Feign 调用 try { MessageSendResp resp messageClient.sendMessage(req); System.out.println(发送成功: resp); } catch (Exception e) { System.err.println(发送失败: e.getMessage()); } }八、总结步骤操作1使用 Gson / Hutool 构建与 Swagger 格式一致的 JSON 结构2如果是阿里云短信调用toString()转为 JSON 字符串3将该字符串设置到 DTO 的data字段4通过 Feign 发送这样Feign 发送的请求体就与 Swagger 完全一致不会再有序列化错误。