网站源码文件,山东网站建设哪家便宜,电商学习网站,外网代理ip例如获取API数据#xff0c;以下只教思想#xff0c;我下面也是代码也是大概写了写#xff1a;
1.新建两个实例#xff0c;一个是API提供方提供的API返回data#xff0c;一个是本地用于接收的data数据#xff0c;以下两个类虽然后者多几个字段#xff0c;但是除多出那几…例如获取API数据以下只教思想我下面也是代码也是大概写了写
1.新建两个实例一个是API提供方提供的API返回data一个是本地用于接收的data数据以下两个类虽然后者多几个字段但是除多出那几个其他的字段不论大小写必须一致。
// 接口方
Data
public class RYZIdentityResponse{ private String name; private String idCardNo; private String phone; private String addRess; private String birthday;
}
// 接收数据
Data
public class RYZIdCardOcrUploadInfo{ private String id; private String sqbh; private String yypch; private String name; private String idCardNo; private String phone; private String addRess; private String birthday;
}
2.获取API结果
import cn.hutool.core.util.ArrayUtil;
import org.apache.commons.beanutils.MethodUtils;
import org.springframework.beans.BeanUtils;
SyApiResultMapString,String result null;
try{ result SyApiExecuteUtil.httpPost(param,url);
}catch(){ throw new CommonException(ResCode.ERROR_BY.getCode(),result.getMessage());
}
MapString,String map null;
if(result.isOK()){ map result.getResult();
}else{ throw new CommonException(ResCode.ERROR_BY.getCode(),result.getMessage());
}
3.开始进行反射
// 创建实例用于接受上述API中获取到的结果(这是根据接口提供方提供的接受对象)
RYZIdentityResponse invoce new RYZIdentityResponse();
// 获取对象的属性否则对象接口加字段反射会报错 Method[] declaredMethods invoice.getClass().getDeclaredMethods(); String[] strs new String[declaredMethods.length]; for (int i 0; i declaredMethods.length; i) { strs[i] declaredMethods[i].getName(); } for (Map.EntryString,String e : map.entrySet()) { String setter set StringUtil.reString(e.getKey()); //2.判断是否在指定的方法内避免反射报错 if (ArrayUtil.contains(strs, setter)) { String value e.getValue(); MethodUtils.invokeMethod(invoice, setter, value); } }
RYZIdCardOcrUploadInfo invoiceInfo new RYZIdCardOcrUploadInfo(); BeanUtils.copyProperties(invoice,invoiceInfo); invoiceInfo.setSqbh(sqbh); invoiceInfo.setYypch(ecmBusinessCode); IfincarDAOUtils.getIfincarBaseDAO().save(invoiceInfo);
----------------------------------------上面会调用到到的地方-----------------------------------------
Data Accessors(chain true) public class SyApiResultT { private boolean success; private String message; private T result; public static SyApiResult success(){ return new SyApiResult().setSuccess(true); } public static SyApiResult fail(String message){ return new SyApiResult().setMessage(message); } }
public class StringUtil{ /** * 将首字母或者_开头的字母转换为大写 * param str * return */ public static String reString(String str) { // 判断传入的字符串是否为空 if (StringUtils.isBlank(str)) { return ; } str str.toLowerCase(); str str.substring(0, 1).toUpperCase() str.substring(1); // 判断传入的字符串是否包含下划线如果不包含直接返回 if (!str.contains(_)) { return str; } // 将符合条件的字符串进行拆分 String[] splitArr str.split(_); String change ; String newStr splitArr[0]; // 遍历数组 for (int i 1; i splitArr.length; i) { String substring splitArr[i].substring(0, 1); change splitArr[i].replaceFirst(substring, substring.toUpperCase(Locale.ROOT)); newStr change; } return newStr; }
}
.