java接口入参模板化,适用于企业化服务远程调度模板化的场景,接口入参实现高度可配置化
需求:远程服务接口模板化配置提供接入服务
模板接口分为三个模块:功能路由、参数校验、模板入库
路由:这里的实现方式很简单,就是根据业务标识找到对应的处理方法
参数校验:
参数校验这步涉及模板和校验类两个核心模块
提供一个表设计:
CREATE TABLE `account_template_validation` (
`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`city_no` VARCHAR(50) NOT NULL COMMENT '城市编码',
`template_id` VARCHAR(64) NOT NULL COMMENT '模板id',
`properties` VARCHAR(4000) NOT NULL COMMENT '模板',
`isReturn` VARCHAR(10) NOT NULL COMMENT '是否要跳转',
`create_time` DATETIME DEFAULT NULL COMMENT '创建时间',
`update_time` DATETIME DEFAULT NULL COMMENT '更新时间',
PRIMARY KEY (`id`),
INDEX `cityNo` (`city_no`),
INDEX `templateId` (`template_id`)
) ENGINE=INNODB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8
模板表模板的值需要用到json格式的报文:
{
"A": {
"notNull": "false",
"length": "15",
"toFiled": "B"
},
"A": {
"notNull": "false",
"length": "15",
"toFiled": "C"
},
"gmt_time": {
"notNull": "false",
"length": "15",
"toFiled": "gmtTime",
"formate": "((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})(((0[13578]|1[02])(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)(0[1-9]|[12][0-9]|30))|(02(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))0229))([0-1][0-9]|2[0-3])([0-5][0-9])([0-5][0-9])$"
}
}
模板的报文是包含入参字段配置的属性
核心校验类:
public static void main(String[] args) { JSONObject template = JSON.parseObject("{\"si_type\":{\"notNull\":\"true\",\"length\":\"32\",\"toFiled\":\"siType\"},\"account_balance\":{\"notNull\":\"false\",\"length\":\"15\",\"toFiled\":\"accountBalance\"},\"pay_cost_base\":{\"notNull\":\"false\",\"length\":\"15\",\"toFiled\":\"payCostBase\"},\"pay_cost_month\":{\"notNull\":\"true\",\"length\":\"6\",\"toFiled\":\"payCostMonth\"},\"pay_cost_total\":{\"notNull\":\"false\",\"length\":\"15\",\"toFiled\":\"payCostTotal\"},\"pay_cost_person\":{\"notNull\":\"false\",\"length\":\"15\",\"toFiled\":\"payCostPerson\"},\"pay_cost_unit\":{\"notNull\":\"false\",\"length\":\"15\",\"toFiled\":\"payCostUnit\"},\"account_person\":{\"notNull\":\"false\",\"length\":\"15\",\"toFiled\":\"accountPerson\"},\"extend_params_test1\":{\"notNull\":\"false\",\"length\":\"15\",\"toFiled\":\"extend_params\"},\"extend_params_test2\":{\"notNull\":\"false\",\"length\":\"15\",\"toFiled\":\"extend_params\"}}"); JSONObject sourceObject = JSON.parseObject("{\"biz_type\":\"si_payment_incomeS\",\"city_code\":\"330100\",\"sys_service_provider_id\":\"2088101010464092\",\"si_data\":[{\"target_idcard_type\":\"01\",\"request_id\":\"201901311817129355\",\"target_idcard\":\"4453231994089898631\",\"target_user_name\":\"张三\",\"target_user_id\":\"2088102122001010\",\"target_sicard_no\":\"M40000240\",\"target_extend_params\":\"{}\",\"mobile\":\"18100000000\",\"template_id\":\"XS_si_payment_income_template\",\"template_version\":\"1.0\",\"template_data\":[{\"account_balance\":\"0\",\"account_person\":\"40\",\"pay_cost_month\":\"195504\",\"pay_cost_base\":\"2500\",\"pay_cost_total\":\"195\",\"pay_cost_person\":\"195\",\"pay_cost_unit\":\"1955\",\"extend_params\":\"{}\",\"si_type\":\"10\"},{\"account_balance\":\"0\",\"account_person\":\"40\",\"pay_cost_month\":\"195504\",\"pay_cost_base\":\"2500\",\"pay_cost_total\":\"195\",\"pay_cost_person\":\"195\",\"pay_cost_unit\":\"1955\",\"extend_params\":\"{}\",\"si_type\":\"20\"},{\"account_balance\":\"0\",\"account_person\":\"40\",\"pay_cost_month\":\"195504\",\"pay_cost_base\":\"2500\",\"pay_cost_total\":\"195\",\"pay_cost_person\":\"195\",\"pay_cost_unit\":\"1955\",\"extend_params\":\"{}\",\"si_type\":\"30\"},{\"account_balance\":\"0\",\"account_person\":\"40\",\"pay_cost_month\":\"195504\",\"pay_cost_base\":\"2500\",\"pay_cost_total\":\"195\",\"pay_cost_person\":\"195\",\"pay_cost_unit\":\"1955\",\"extend_params\":\"{}\",\"si_type\":\"40\"}],\"service_return_url\":\"https://www.alipay.com\"}],\"extend_params\":\"{}\",\"target_notify_time\":\"20181226151421\"}"); *//** * 开始校验数据 * 需要校验数据的长度,是否必传,是否满足正则表达式规则. *//* String siArry = sourceObject.getString("si_data"); JSONArray siarr = JSON.parseArray(siArry); for (Object object : siarr) { String siData = object == null ? "" : String.valueOf(object); JSONObject siObject = JSON.parseObject(siData); String tempdataArry = siObject.getString("template_data"); JSONArray tempdataarr = JSON.parseArray(tempdataArry); for (Object object2 : tempdataarr) { String tempData = object2 == null ? "" : String.valueOf(object2); JSONObject tempObject = JSON.parseObject(tempData); Set<Entry<String, Object>> entrySet = tempObject.entrySet(); for (Entry<String, Object> entry : entrySet) { String key = entry.getKey(); Object value = entry.getValue(); System.out.println(key+"---------"+value); TemplateValidate templateValidator = JSON.parseObject((String) template.getString(key), TemplateValidate.class); System.out.println(templateValidator); //判断是否为空 if ("true".equals(templateValidator.getNotNull())) { if (value == null) { System.out.println("{0},校验失败!不能为null!"); LoggerUtil.info(logger, "{0},校验失败!不能为null!", key); } else if (StringUtil.isBlank(String.valueOf(value))) { System.out.println("{0},校验失败!不能为null!"); LoggerUtil.info(logger, "{0},校验失败!不能为null!", key); } } //判断长度是否超过规定值 Integer length = Integer.parseInt(templateValidator.getLength()); if (((String) value).length() > length) { System.out.println("{0},校验失败!长度超长!"); LoggerUtil.info(logger, "{0},校验失败!长度超长!", key); } //判断格式是否正确 if (StringUtil.isNotBlank(templateValidator.getFormate())) { if (!Pattern.matches(templateValidator.getFormate(), String.valueOf(value))) { LoggerUtil.info(logger, "{0},校验失败!格式不匹配!", key); } } } } } }
class TemplateValidate {
private String toFiled;
private String notNull;
private String length;
private String formate;
public String getToFiled() {
return toFiled;
}
public void setToFiled(String toFiled) {
this.toFiled = toFiled;
}
public String getNotNull() {
return notNull;
}
public void setNotNull(String notNull) {
this.notNull = notNull;
}
public String getLength() {
return length;
}
public void setLength(String length) {
this.length = length;
}
public String getFormate() {
return formate;
}
public void setFormate(String formate) {
this.formate = formate;
}
}
模板参数组装:
public static void main(String[] args) { JSONObject template = JSON.parseObject( "{\"si_type\":{\"notNull\":\"true\",\"length\":\"32\",\"toFiled\":\"si_type\",\"formate\":\"[12345]0\"},\"account_balance\":{\"notNull\":\"false\",\"length\":\"15\",\"toFiled\":\"account_balance\"},\"pay_cost_base\":{\"notNull\":\"false\",\"length\":\"15\",\"toFiled\":\"pay_cost_base\"},\"month\":{\"notNull\":\"true\",\"length\":\"6\",\"toFiled\":\"pay_cost_month\",\"formate\":\"(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})((0[13578]|1[02])|(0[469]|11)|02))\"},\"pay_cost_total\":{\"notNull\":\"false\",\"length\":\"15\",\"toFiled\":\"pay_cost_total\"},\"pay_cost_person\":{\"notNull\":\"false\",\"length\":\"15\",\"toFiled\":\"pay_cost_person\"},\"pay_cost_unit\":{\"notNull\":\"false\",\"length\":\"15\",\"toFiled\":\"pay_cost_unit\"},\"account_person\":{\"notNull\":\"false\",\"length\":\"15\",\"toFiled\":\"account_person\"},\"extend_params\":{\"notNull\":\"false\",\"length\":\"15\",\"toFiled\":\"extend_params\"}}"); JSONObject sourceObject = JSON.parseObject( "{\"biz_type\":\"si_payment_incomeS\",\"city_code\":\"330100\",\"sys_service_provider_id\":\"2088101010464092\",\"si_data\":[{\"target_idcard_type\":\"01\",\"request_id\":\"201902011759182132\",\"target_idcard\":\"445323199404211217\",\"target_user_name\":\"张三\",\"target_user_id\":\"2088102122001010\",\"target_sicard_no\":\"M40000240\",\"target_extend_params\":\"{}\",\"mobile\":\"18100000000\",\"template_id\":\"XS_si_payment_income_template\",\"template_version\":\"1.0\",\"template_data\":[{\"account_balance\":\"0\",\"account_person\":\"50\",\"month\":\"201902\",\"pay_cost_base\":\"2500\",\"pay_cost_total\":\"195\",\"pay_cost_person\":\"195\",\"pay_cost_unit\":\"1955\",\"extend_params\":\"{}\",\"si_type\":\"10\"},{\"account_balance\":\"0\",\"account_person\":\"40\",\"month\":\"201902\",\"pay_cost_base\":\"2500\",\"pay_cost_total\":\"195\",\"pay_cost_person\":\"195\",\"pay_cost_unit\":\"1955\",\"extend_params\":\"{}\",\"si_type\":\"20\"},{\"account_balance\":\"0\",\"account_person\":\"40\",\"month\":\"201902\",\"pay_cost_base\":\"2500\",\"pay_cost_total\":\"195\",\"pay_cost_person\":\"195\",\"pay_cost_unit\":\"1955\",\"extend_params\":\"{}\",\"si_type\":\"30\"},{\"account_balance\":\"0\",\"account_person\":\"40\",\"month\":\"201902\",\"pay_cost_base\":\"2500\",\"pay_cost_total\":\"195\",\"pay_cost_person\":\"195\",\"pay_cost_unit\":\"1955\",\"extend_params\":\"{}\",\"si_type\":\"40\"}],\"service_return_url\":\"https://www.alipay.com\"}],\"extend_params\":\"{}\",\"target_notify_time\":\"20181226151421\"}"); String siArry = sourceObject.getString("si_data"); sourceObject.remove("si_data"); JSONArray siarr = JSON.parseArray(siArry); JSONArray toSiArr=new JSONArray(); for (Object object : siarr) { String siData = object == null ? "" : String.valueOf(object); JSONObject siObject = JSON.parseObject(siData); String tempdataArry = siObject.getString("template_data"); JSONArray tempdataarr = JSON.parseArray(tempdataArry); JSONArray totempdataarr = new JSONArray(); for (Object objecttemp : tempdataarr) { String tempData = objecttemp == null ? "" : String.valueOf(objecttemp); JSONObject tempObject = JSON.parseObject(tempData); JSONObject toObject = new JSONObject(); Set<Entry<String, Object>> entrySet = tempObject.entrySet(); for (Entry<String, Object> entry : entrySet) { String key = entry.getKey(); Object value = entry.getValue(); TemplateValidate templateValidator = JSON.parseObject((String) template.getString(key), TemplateValidate.class); if ("extend_params".equals(templateValidator.getToFiled())) { toObject.put( "extendParams", (toObject.getString("extendParams") == null ? "" : toObject.getString("extendParams")) + "{\"" + key + "\":\"" + value + "\"}"); } toObject.put(templateValidator.getToFiled(), value); } totempdataarr.add(toObject); } siObject.remove("template_data"); siObject.put("template_data", totempdataarr); toSiArr.add(siObject); } sourceObject.put("si_data", toSiArr); SiAccountIncomeRequestBodyVO requestVO = JSON.toJavaObject(sourceObject, SiAccountIncomeRequestBodyVO.class); System.out.println(requestVO); handel(requestVO);//处理业务,后续落库 }