From 69a3c374ca3afb770e3b9ffcbdda07ce362cbf58 Mon Sep 17 00:00:00 2001
From: 1 <1@123>
Date: 星期五, 09 一月 2026 19:59:29 +0800
Subject: [PATCH] #
---
rsf-open-api/src/main/java/com/vincent/rsf/openApi/controller/example/JsonReplaceExampleController.java | 326 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 326 insertions(+), 0 deletions(-)
diff --git a/rsf-open-api/src/main/java/com/vincent/rsf/openApi/controller/example/JsonReplaceExampleController.java b/rsf-open-api/src/main/java/com/vincent/rsf/openApi/controller/example/JsonReplaceExampleController.java
new file mode 100644
index 0000000..e1e8fbf
--- /dev/null
+++ b/rsf-open-api/src/main/java/com/vincent/rsf/openApi/controller/example/JsonReplaceExampleController.java
@@ -0,0 +1,326 @@
+package com.vincent.rsf.openApi.controller.example;
+
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import com.vincent.rsf.openApi.entity.dto.CommonResponse;
+import com.vincent.rsf.openApi.utils.ParamsMapUtils;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * JSON灞炴�у悕閫掑綊鏇挎崲浣跨敤绀轰緥
+ *
+ * 婕旂ず濡備綍浣跨敤 FuncMap 閫掑綊閬嶅巻骞舵浛鎹SON鎵�鏈夊眰绾х殑灞炴�у悕绉�
+ *
+ * @author vincent
+ * @since 2026-01-04
+ */
+@Slf4j
+@RestController
+@RequestMapping("/example/json-replace")
+@Api(tags = "JSON灞炴�ч�掑綊鏇挎崲绀轰緥")
+public class JsonReplaceExampleController {
+
+ /**
+ * 绀轰緥1锛氱畝鍗曞璞$殑灞炴�ф浛鎹�
+ *
+ * 杈撳叆锛�
+ * {
+ * "orderNumber": "PO001",
+ * "orderQty": 100,
+ * "orderAmount": 5000.00
+ * }
+ *
+ * 杈撳嚭锛�
+ * {
+ * "code": "PO001",
+ * "qty": 100,
+ * "anfme": 5000.00
+ * }
+ */
+ @ApiOperation("绀轰緥1锛氱畝鍗曞璞″睘鎬ф浛鎹�")
+ @PostMapping("/simple-replace")
+ public CommonResponse simpleReplace(@RequestBody JSONObject data) {
+ log.info("鍘熷鏁版嵁锛歿}", data);
+
+ // 瀹氫箟鏄犲皠瑙勫垯
+ Map<String, String> mappingRules = new HashMap<>();
+ mappingRules.put("orderNumber", "code");
+ mappingRules.put("orderQty", "qty");
+ mappingRules.put("orderAmount", "anfme");
+
+ // 鎵ц鏇挎崲
+ JSONObject result = ParamsMapUtils.replaceJsonKeys(data, mappingRules);
+
+ log.info("鏇挎崲鍚庢暟鎹細{}", result);
+
+ return CommonResponse.ok()
+ .setMsg("鏇挎崲鎴愬姛")
+ .setData(result);
+ }
+
+ /**
+ * 绀轰緥2锛氬祵濂楀璞$殑娣卞害鏇挎崲
+ *
+ * 杈撳叆锛�
+ * {
+ * "orderNumber": "PO001",
+ * "customer": {
+ * "customerName": "寮犱笁",
+ * "customerPhone": "13800138000",
+ * "address": {
+ * "cityName": "鍖椾含",
+ * "streetName": "鏈濋槼璺�"
+ * }
+ * }
+ * }
+ *
+ * 杈撳嚭锛�
+ * {
+ * "code": "PO001",
+ * "customer": {
+ * "name": "寮犱笁",
+ * "phone": "13800138000",
+ * "address": {
+ * "city": "鍖椾含",
+ * "street": "鏈濋槼璺�"
+ * }
+ * }
+ * }
+ */
+ @ApiOperation("绀轰緥2锛氬祵濂楀璞℃繁搴︽浛鎹�")
+ @PostMapping("/nested-replace")
+ public CommonResponse nestedReplace(@RequestBody JSONObject data) {
+ log.info("鍘熷宓屽鏁版嵁锛歿}", data);
+
+ // 瀹氫箟鏄犲皠瑙勫垯锛堥�傜敤浜庢墍鏈夊眰绾э級
+ Map<String, String> mappingRules = new HashMap<>();
+ mappingRules.put("orderNumber", "code");
+ mappingRules.put("customerName", "name");
+ mappingRules.put("customerPhone", "phone");
+ mappingRules.put("cityName", "city");
+ mappingRules.put("streetName", "street");
+
+ // 閫掑綊鏇挎崲鎵�鏈夊眰绾�
+ JSONObject result = ParamsMapUtils.replaceJsonKeys(data, mappingRules);
+
+ log.info("鏇挎崲鍚庡祵濂楁暟鎹細{}", result);
+
+ return CommonResponse.ok()
+ .setMsg("宓屽鏇挎崲鎴愬姛")
+ .setData(result);
+ }
+
+ /**
+ * 绀轰緥3锛氭暟缁勫璞$殑鎵归噺鏇挎崲
+ *
+ * 杈撳叆锛�
+ * {
+ * "orderNumber": "PO001",
+ * "items": [
+ * {
+ * "materialCode": "MAT001",
+ * "materialName": "鐗╂枡A",
+ * "itemQty": 10
+ * },
+ * {
+ * "materialCode": "MAT002",
+ * "materialName": "鐗╂枡B",
+ * "itemQty": 20
+ * }
+ * ]
+ * }
+ *
+ * 杈撳嚭锛�
+ * {
+ * "code": "PO001",
+ * "items": [
+ * {
+ * "matnr": "MAT001",
+ * "maktx": "鐗╂枡A",
+ * "qty": 10
+ * },
+ * {
+ * "matnr": "MAT002",
+ * "maktx": "鐗╂枡B",
+ * "qty": 20
+ * }
+ * ]
+ * }
+ */
+ @ApiOperation("绀轰緥3锛氭暟缁勫璞℃壒閲忔浛鎹�")
+ @PostMapping("/array-replace")
+ public CommonResponse arrayReplace(@RequestBody JSONObject data) {
+ log.info("鍘熷鏁扮粍鏁版嵁锛歿}", data);
+
+ // 瀹氫箟鏄犲皠瑙勫垯
+ Map<String, String> mappingRules = new HashMap<>();
+ mappingRules.put("orderNumber", "code");
+ mappingRules.put("materialCode", "matnr");
+ mappingRules.put("materialName", "maktx");
+ mappingRules.put("itemQty", "qty");
+
+ // 閫掑綊鏇挎崲锛堝寘鎷暟缁勪腑鐨勬墍鏈夊璞★級
+ JSONObject result = ParamsMapUtils.replaceJsonKeys(data, mappingRules);
+
+ log.info("鏇挎崲鍚庢暟缁勬暟鎹細{}", result);
+
+ return CommonResponse.ok()
+ .setMsg("鏁扮粍鏇挎崲鎴愬姛")
+ .setData(result);
+ }
+
+ /**
+ * 绀轰緥4锛氬鏉傜粨鏋勭殑瀹屾暣鏇挎崲
+ *
+ * 杈撳叆鍖呭惈锛氬祵濂楀璞� + 鏁扮粍 + 澶氬眰宓屽
+ */
+ @ApiOperation("绀轰緥4锛氬鏉傜粨鏋勫畬鏁存浛鎹�")
+ @PostMapping("/complex-replace")
+ public CommonResponse complexReplace(@RequestBody JSONObject data) {
+ log.info("鍘熷澶嶆潅鏁版嵁锛歿}", data);
+
+ // 瀹氫箟瀹屾暣鐨勬槧灏勮鍒�
+ Map<String, String> mappingRules = new HashMap<>();
+ // 璁㈠崟瀛楁
+ mappingRules.put("orderNumber", "code");
+ mappingRules.put("orderType", "type");
+ mappingRules.put("orderQty", "qty");
+ mappingRules.put("orderAmount", "anfme");
+ mappingRules.put("orderStatus", "exceStatus");
+
+ // 瀹㈡埛瀛楁
+ mappingRules.put("customerName", "custName");
+ mappingRules.put("customerCode", "custCode");
+
+ // 鐗╂枡瀛楁
+ mappingRules.put("materialCode", "matnr");
+ mappingRules.put("materialName", "maktx");
+ mappingRules.put("materialSpec", "spec");
+ mappingRules.put("materialUnit", "meins");
+
+ // 鍏朵粬瀛楁
+ mappingRules.put("warehouseCode", "whCode");
+ mappingRules.put("locationCode", "locCode");
+
+ // 鎵ц閫掑綊鏇挎崲
+ JSONObject result = ParamsMapUtils.replaceJsonKeys(data, mappingRules);
+
+ log.info("鏇挎崲鍚庡鏉傛暟鎹細{}", result);
+
+ return CommonResponse.ok()
+ .setMsg("澶嶆潅鏇挎崲鎴愬姛")
+ .setData(result);
+ }
+
+ /**
+ * 绀轰緥5锛氫娇鐢╝ppId鍜宖uncId鑷姩鏄犲皠
+ *
+ * 杩欎釜绀轰緥灞曠ず濡備綍缁撳悎鏁版嵁搴撻厤缃嚜鍔ㄨ繘琛屽瓧娈垫槧灏�
+ */
+ @ApiOperation("绀轰緥5锛氫娇鐢ㄩ厤缃嚜鍔ㄦ槧灏�")
+ @PostMapping("/auto-replace")
+ public CommonResponse autoReplace(@RequestBody JSONObject request) {
+ String appId = request.getString("appId");
+ String funcId = request.getString("funcId");
+ JSONObject data = request.getJSONObject("data");
+
+ log.info("鑷姩鏄犲皠 - appId:{}, funcId:{}, 鏁版嵁:{}", appId, funcId, data);
+
+ // 浣跨敤 FuncMap 鐨� apiMaps 鏂规硶锛屽畠浼氳嚜鍔ㄤ粠鏁版嵁搴撳姞杞芥槧灏勮鍒欏苟閫掑綊鏇挎崲
+ JSONObject result = ParamsMapUtils.apiMaps(appId, funcId, data);
+
+ log.info("鑷姩鏄犲皠鍚庢暟鎹細{}", result);
+
+ return CommonResponse.ok()
+ .setMsg("鑷姩鏄犲皠鎴愬姛")
+ .setData(result);
+ }
+
+ /**
+ * 绀轰緥6锛氱洿鎺ユ浛鎹SON鏁扮粍
+ */
+ @ApiOperation("绀轰緥6锛氱洿鎺ユ浛鎹SON鏁扮粍")
+ @PostMapping("/array-direct-replace")
+ public CommonResponse arrayDirectReplace(@RequestBody JSONArray array) {
+ log.info("鍘熷JSON鏁扮粍锛歿}", array);
+
+ // 瀹氫箟鏄犲皠瑙勫垯
+ Map<String, String> mappingRules = new HashMap<>();
+ mappingRules.put("materialCode", "matnr");
+ mappingRules.put("materialName", "maktx");
+ mappingRules.put("stockQty", "qty");
+ mappingRules.put("warehouseCode", "whCode");
+
+ // 鐩存帴鏇挎崲鏁扮粍
+ JSONArray result = ParamsMapUtils.replaceJsonArrayKeys(array, mappingRules);
+
+ log.info("鏇挎崲鍚嶫SON鏁扮粍锛歿}", result);
+
+ return CommonResponse.ok()
+ .setMsg("鏁扮粍鐩存帴鏇挎崲鎴愬姛")
+ .setData(result);
+ }
+
+ /**
+ * 娴嬭瘯鐢ㄤ緥锛氱敓鎴愮ず渚嬫暟鎹�
+ */
+ @ApiOperation("鐢熸垚娴嬭瘯鏁版嵁")
+ @PostMapping("/generate-test-data")
+ public CommonResponse generateTestData() {
+ // 鍒涘缓澶嶆潅鐨勬祴璇曟暟鎹�
+ JSONObject testData = new JSONObject();
+ testData.put("orderNumber", "PO20260104001");
+ testData.put("orderType", "PURCHASE");
+ testData.put("orderQty", 500);
+ testData.put("orderAmount", 25000.00);
+ testData.put("orderStatus", "PENDING");
+
+ // 瀹㈡埛淇℃伅
+ JSONObject customer = new JSONObject();
+ customer.put("customerCode", "CUST001");
+ customer.put("customerName", "鍖椾含绉戞妧鏈夐檺鍏徃");
+
+ JSONObject address = new JSONObject();
+ address.put("cityName", "鍖椾含");
+ address.put("districtName", "鏈濋槼鍖�");
+ address.put("streetName", "寤哄浗璺�88鍙�");
+ customer.put("address", address);
+
+ testData.put("customer", customer);
+
+ // 璁㈠崟鏄庣粏
+ JSONArray items = new JSONArray();
+ for (int i = 1; i <= 3; i++) {
+ JSONObject item = new JSONObject();
+ item.put("itemNo", i);
+ item.put("materialCode", "MAT00" + i);
+ item.put("materialName", "娴嬭瘯鐗╂枡" + i);
+ item.put("materialSpec", "瑙勬牸" + i);
+ item.put("materialUnit", "EA");
+ item.put("itemQty", 100 + i * 50);
+ item.put("itemAmount", 5000.00 + i * 1000);
+
+ // 浠撳簱淇℃伅
+ JSONObject warehouse = new JSONObject();
+ warehouse.put("warehouseCode", "WH0" + i);
+ warehouse.put("locationCode", "LOC-A-0" + i);
+ item.put("warehouse", warehouse);
+
+ items.add(item);
+ }
+ testData.put("items", items);
+
+ return CommonResponse.ok()
+ .setMsg("娴嬭瘯鏁版嵁鐢熸垚鎴愬姛")
+ .setData(testData);
+ }
+}
--
Gitblit v1.9.1