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);
|
}
|
}
|