使用反射获取API数据并强制转为实例

例如获取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;

SyApiResult> result = null;

try{

        result = SyApiExecuteUtil.httpPost(param,url);

}catch(){

        throw new CommonException(ResCode.ERROR_BY.getCode(),result.getMessage());

}

Map 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.Entry 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 SyApiResult {

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

}

.

你可能感兴趣的:(java,后端)