java高级反射(一),通过反射批量修改属性值set,get方法 Field,对属性值进行统一操作

通过反射批量修改属性值set,get方法 Field

需求说明:对接一个系统,对方系统要求每个字段value值加密,加密方法为WebAppAESUtil.encrypt,做之前就想做一个对属性统一操作的一个方法,反射完美的解决了这个问题,类似需求可以这样操作,下面是代码,反射是个好东西

反射工具类
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * 类 名: FieldReflection
 * 描 述:
 * 作 者: binglong180
 * 创 建: 2020-07-02 11:23
 * 邮 箱: binglong172@163.com
 */
@Slf4j
public class ReflectionUtils {

  public static Object  FieldReflection(Object oldObj) throws Exception{
    // 获取对象所有的实例域
    Field[] fields = oldObj.getClass().getDeclaredFields();
    // 获得访问私有实例域的权限
    AccessibleObject.setAccessible(fields, true);
    for (Field field : fields) {
      Class<?> type = field.getType();
      if ("String".equals(type.getSimpleName())) {
        Object obj = field.get(oldObj);
        if (obj != null) {
            String str = obj.toString();
            field.set(oldObj,WebAppAESUtil.encrypt(str));
        }
      }


      if ("CompanyDTO".equals(type.getSimpleName())){
        CompanyDTO companyDTO = (CompanyDTO) field.get(oldObj);
        if (companyDTO != null){
          companyDTO = (CompanyDTO) FieldReflection(companyDTO);
          field.set(oldObj,companyDTO);
        }
      }

      if ("List".equals(type.getSimpleName())){
        List<UserInfoDTO> userInfoDTOList = (List<UserInfoDTO>) field.get(oldObj);
        if (userInfoDTOList != null && userInfoDTOList.size()>0){
          List<UserInfoDTO> userInfoList = new ArrayList<>();
          for (UserInfoDTO item: userInfoDTOList ){
            if (item != null){
              item = (UserInfoDTO) FieldReflection(item);
              userInfoList.add(item);
            }
          }
          field.set(oldObj,userInfoList);
        }
      }

     
    }
    return oldObj;
  }


}
1、ServiceDTO 实体类 有List 属性 及CompanyDTO 属性
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 服务申请接口请求参数字段
 */
@Data
public class ServiceDTO {

    private CompanyDTO nytsydcompanyrequest;//用电企业 必选:true

    private List<UserInfoDTO> nytsuserrequest;//用户 必选:true nytsuserrequest;//电站信息表 必选:true

    private NytsPartnetDTO nytsPartnet;

    private String debUserId;//电e宝用户编号 必选:true

    private String debSessionId;//会话id 必选:true

    private String timeStamp;//当前时间的毫秒值 必选:true

    private String discountType;//优惠类型 必选:true 1固定折扣金额 ,2无折扣(固定电价),3固定优惠金额 ,4先减再乘,5先乘再减。

    private String status;//状态 必选:true 1正常 2失效

    private String startTime;//合同开始日期 必选:true 取合同相关信息

    private String endTime;//合同结束日期 必选:true 取合同相关信息

    private String billDay;//账单日必选:true

    private String pvTopPrice;//光伏支付工厂尖电价 必选:true 录入

    private String pvPeakPrice;//光伏支付工厂峰电价 必选:true 录入

    private String pvFlatPrice;//光伏支付工厂平电价 必选:true 录入

    private String pvBottomPrice;//光伏支付工谷尖电价 必选:true 录入

    private String factoryTopPrice;//工厂支付光伏尖电价 必选:true 无信息,不传递

    private String factoryPeakPrice;//工厂支付光伏尖电价 必选:true 无信息,不传递

    private String factoryFlatPrice;//工厂支付光伏尖电价 必选:true 无信息,不传递

    private String factoryBottomPrice;//工厂支付光伏尖电价 必选:true 无信息,不传递

    private String fdHouseNumber;//发电户号 必选:true 录入

    private String preferential;//优惠(减多少) 必选:true 默认初始值:0

    private String discount;//折扣(打几折乘以多少)

    private String signTime;//合同签订日期

    private String image;//合同附件 格式为png转base64

    private String remark;//合同说明

    private String lastUpdateTime;//上次更新时间

    private String ydHouseNumber;//用电户号录入

    public static ServiceDTO getInitModel(){
        ServiceDTO serviceVo = new ServiceDTO();
        serviceVo.setChannelType("03");
        serviceVo.setStatus("1");
        serviceVo.setDiscountType("2");
        serviceVo.setPreferential("0");
        long timeMillis = System.currentTimeMillis();
        serviceVo.setTimeStamp(String.valueOf(timeMillis));
       // String token = cacheStore.getTokenCache().get(current.getId());
       // serviceVo.setDebSessionId(token);
        return serviceVo;
    }
}
CompanyDTO 类
import lombok.Data;

/**
 * 用电企业字段
 */
@Data
public class CompanyDTO {

   private String companyType;//公司类型 必选:true 1发电企业 2用电企业

   private String companyName;//公司名称 必选:true 取业主名称(录入或合同)

   private String groupCompany;//集团公司 必选:true 1隶属集团公司,2不是集团公司

   private String bankName;//开户行名称 必选:true

   private String orgCode;//组织信用代码(统一社会信用代码) 必选:true 取业主统一社会信用代码(录入或企业认证)

   private String bankAccount;//银行账户 必选:true

   private String address;//公司地址

   private String legalPerson;//公司法人 取业主法人姓名(录入或企业认证)

   private String telPhone;//公司联系电话

   private String province;//省 取企业认证申请时录入的相关信息

   private String city;//市 取企业认证申请时录入的相关信息

   private String area;//区 取企业认证申请时录入的相关信息

}

运行后效果
加密前:
{
	"billDay": null,
	"channelType": "03",
	"debSessionId": null,
	"debUserId": "00158368",
	"discount": null,
	"discountType": "2",
	"endTime": "2020-06-01",
	"factoryBottomPrice": null,
	"factoryFlatPrice": null,
	"factoryPeakPrice": null,
	"factoryTopPrice": null,
	"fdHouseNumber": "132",
	"image": null,
	"lastUpdateTime": null,
	"nytsPartnet": {
		"partnetname": ""
	},
	"nytsfdcompanyrequest": {
		"address": "天津市,市辖区,和平区",
		"area": null,
		"bankAccount": "6272000020006544121",
		"bankName": "工商银行",
		"city": null,
		"companyName": "上海御漏管道工程有限公司",
		"companyType": "1",
		"groupCompany": "2",
		"legalPerson": "兰兰",
		"orgCode": "310118003050098",
		"province": null,
		"telPhone": "13351111111"
	},
	"nytspowerstationrequest": {
		"address": "123",
		"areaCode": "00168361",
		"capacity": "10000.0000",
		"fdHouseNumber": null,
		"nytspowerstationservicerequest": {
			"orderTime": null,
			"servicePeriod": "10",
			"status": "1",
			"termValidity": "2030-06-16",
			"unitPrice": null
		},
		"stationName": null,
		"stationType": null
	},
	"nytsuserrequest": [{
		"address": null,
		"birthday": null,
		"contentType": "1",
		"email": null,
		"idCard": null,
		"idCorwardImg": null,
		"idReverseImg": null,
		"nation": null,
		"nickName": null,
		"phone": "13351111111",
		"photoUrl": null,
		"realName": null,
		"sex": null,
		"userType": "00",
		"wechat": null
	}, {
		"address": null,
		"birthday": null,
		"contentType": "2",
		"email": null,
		"idCard": null,
		"idCorwardImg": null,
		"idReverseImg": null,
		"nation": null,
		"nickName": null,
		"phone": "13351111111",
		"photoUrl": null,
		"realName": null,
		"sex": null,
		"userType": "00",
		"wechat": null
	}],
	"nytsydcompanyrequest": {
		"address": "天津市,市辖区,和平区",
		"area": null,
		"bankAccount": "6272000020006544121",
		"bankName": "工商银行",
		"city": null,
		"companyName": "上海御漏管道工程有限公司",
		"companyType": "2",
		"groupCompany": "2",
		"legalPerson": "兰兰",
		"orgCode": "310118003050098",
		"province": null,
		"telPhone": "13351111111"
	},
	"preferential": "0",
	"pvBottomPrice": "10.0",
	"pvFlatPrice": "10.0",
	"pvPeakPrice": "10.0",
	"pvTopPrice": "10.0",
	"remark": null,
	"signTime": null,
	"startTime": "2020-05-01",
	"status": "1",
	"timeStamp": "1593655856654",
	"ydHouseNumber": "132"
}

加密后

{
	"billDay": null,
	"channelType": "fcc1b6a60f46587174758d8732e8e462",
	"debSessionId": null,
	"debUserId": "9a47daef5afeae59dd2743c0004c4bba",
	"discount": null,
	"discountType": "1a3da64489b8c6722dafbad407691469",
	"endTime": "171ae7996c4e06809d6ba84537b50c4e",
	"factoryBottomPrice": null,
	"factoryFlatPrice": null,
	"factoryPeakPrice": null,
	"factoryTopPrice": null,
	"fdHouseNumber": "01e6db51b86d93ab6c3e82aa7615517d",
	"image": null,
	"lastUpdateTime": null,
	"nytsPartnet": {
		"partnetname": "f9f5f4174d957aea15993b7e0d513ae6"
	},
	"nytsfdcompanyrequest": {
		"address": "b5cb9ce0779ef9eec64cb929c0304d99bb4620cffea9fa9176274ac07034aae6",
		"area": null,
		"bankAccount": "4adc65a091aa74a60b4c311f697a39db57782e365ff7b846416d0c6463551d3d",
		"bankName": "3a2519ef5285fb116fd38c111f4bb4c6",
		"city": null,
		"companyName": "4a43479c06b3a226eb8fbea379097a1194296bb2ef4fca20beb000c34c8d31ca3d1243347193bdc44070027ad9116854",
		"companyType": "8823fb5b75bf0b2b4b19a6d85e3bfce4",
		"groupCompany": "1a3da64489b8c6722dafbad407691469",
		"legalPerson": "a96ae23fa6279259a481f98c0272df84",
		"orgCode": "91b387200bc3e768c33ef86404a251ee",
		"province": null,
		"telPhone": "f244afac5cb1e511b3ebc51b2982b322"
	},
	"nytspowerstationrequest": {
		"address": "bb9e08229faca7138fc2e64f5d5e58af",
		"areaCode": "f751ecf3877db29f985c2249788863ff",
		"capacity": "cdb31240a0568d01ff9df18e46a11499",
		"fdHouseNumber": null,
		"nytspowerstationservicerequest": {
			"orderTime": null,
			"servicePeriod": "890bb0e5fc386329d9e582bd600ccd0e",
			"status": "8823fb5b75bf0b2b4b19a6d85e3bfce4",
			"termValidity": "3b0e0990362adfaf1087d06a63241cb6",
			"unitPrice": null
		},
		"stationName": null,
		"stationType": null
	},
	"nytsuserrequest": [{
		"address": null,
		"birthday": null,
		"contentType": "8823fb5b75bf0b2b4b19a6d85e3bfce4",
		"email": null,
		"idCard": null,
		"idCorwardImg": null,
		"idReverseImg": null,
		"nation": null,
		"nickName": null,
		"phone": "f244afac5cb1e511b3ebc51b2982b322",
		"photoUrl": null,
		"realName": null,
		"sex": null,
		"userType": "751f67e04de34515ef48ab063a68815e",
		"wechat": null
	}, {
		"address": null,
		"birthday": null,
		"contentType": "1a3da64489b8c6722dafbad407691469",
		"email": null,
		"idCard": null,
		"idCorwardImg": null,
		"idReverseImg": null,
		"nation": null,
		"nickName": null,
		"phone": "f244afac5cb1e511b3ebc51b2982b322",
		"photoUrl": null,
		"realName": null,
		"sex": null,
		"userType": "751f67e04de34515ef48ab063a68815e",
		"wechat": null
	}],
	"nytsydcompanyrequest": {
		"address": "b5cb9ce0779ef9eec64cb929c0304d99bb4620cffea9fa9176274ac07034aae6",
		"area": null,
		"bankAccount": "4adc65a091aa74a60b4c311f697a39db57782e365ff7b846416d0c6463551d3d",
		"bankName": "3a2519ef5285fb116fd38c111f4bb4c6",
		"city": null,
		"companyName": "4a43479c06b3a226eb8fbea379097a1194296bb2ef4fca20beb000c34c8d31ca3d1243347193bdc44070027ad9116854",
		"companyType": "1a3da64489b8c6722dafbad407691469",
		"groupCompany": "1a3da64489b8c6722dafbad407691469",
		"legalPerson": "a96ae23fa6279259a481f98c0272df84",
		"orgCode": "91b387200bc3e768c33ef86404a251ee",
		"province": null,
		"telPhone": "f244afac5cb1e511b3ebc51b2982b322"
	},
	"preferential": "4132f8c881031504cb8d0ad0e5ff37a8",
	"pvBottomPrice": "a9321bf5b7bec7702c218852c4a2c545",
	"pvFlatPrice": "a9321bf5b7bec7702c218852c4a2c545",
	"pvPeakPrice": "a9321bf5b7bec7702c218852c4a2c545",
	"pvTopPrice": "a9321bf5b7bec7702c218852c4a2c545",
	"remark": null,
	"signTime": null,
	"startTime": "b5221eabb6ce34d33d3b1bedc2611c59",
	"status": "8823fb5b75bf0b2b4b19a6d85e3bfce4",
	"timeStamp": "c10b4c45d1235af1b48b62c8d1fdc0c1",
	"ydHouseNumber": "01e6db51b86d93ab6c3e82aa7615517d"
}
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值