1
3 天以前 70ecc717c746eef0c4503a19031ada582f5e94d1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
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 递归遍历并替换JSON所有层级的属性名称
 * 
 * @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:使用appId和funcId自动映射
     * 
     * 这个示例展示如何结合数据库配置自动进行字段映射
     */
    @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:直接替换JSON数组
     */
    @ApiOperation("示例6:直接替换JSON数组")
    @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("替换后JSON数组:{}", 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);
    }
}