通过反射批量修改属性值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
;
@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
;
private List
<UserInfoDTO> nytsuserrequest
;
private NytsPartnetDTO nytsPartnet
;
private String debUserId
;
private String debSessionId
;
private String timeStamp
;
private String discountType
;
private String status
;
private String startTime
;
private String endTime
;
private String billDay
;
private String pvTopPrice
;
private String pvPeakPrice
;
private String pvFlatPrice
;
private String pvBottomPrice
;
private String factoryTopPrice
;
private String factoryPeakPrice
;
private String factoryFlatPrice
;
private String factoryBottomPrice
;
private String fdHouseNumber
;
private String preferential
;
private String discount
;
private String signTime
;
private String image
;
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
));
return serviceVo
;
}
}
CompanyDTO 类
import lombok
.Data
;
@Data
public class CompanyDTO {
private String companyType
;
private String companyName
;
private String groupCompany
;
private String bankName
;
private String orgCode
;
private String bankAccount
;
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"
}