java高级反射(一),通过反射批量修改属性值set,get方法 Field,对属性值进行统一操作
生活随笔
收集整理的這篇文章主要介紹了
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;//用電企業 必選:trueprivate List<UserInfoDTO> nytsuserrequest;//用戶 必選:true nytsuserrequest;//電站信息表 必選:trueprivate NytsPartnetDTO nytsPartnet;private String debUserId;//電e寶用戶編號 必選:trueprivate String debSessionId;//會話id 必選:trueprivate String timeStamp;//當前時間的毫秒值 必選:trueprivate 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;//賬單日必選:trueprivate 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 默認初始值:0private String discount;//折扣(打幾折乘以多少)private String signTime;//合同簽訂日期private String image;//合同附件 格式為png轉base64private 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;//開戶行名稱 必選:trueprivate String orgCode;//組織信用代碼(統一社會信用代碼) 必選:true 取業主統一社會信用代碼(錄入或企業認證)private String bankAccount;//銀行賬戶 必選:trueprivate 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" }總結
以上是生活随笔為你收集整理的java高级反射(一),通过反射批量修改属性值set,get方法 Field,对属性值进行统一操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 端口映射教程
- 下一篇: 1.2 极限的性质【极限】