我有bean1
(Getter
和Setter
)有数百个属性,另有bean2
(实施Builder Pattern
即使用Getters
和Builder Pattern
)。
我想将属性从bean1
复制到bean2
。我该怎么做?
注意: - BeanUtils.copyProperties(source, target);
没有使用Builder模式bean。
Bean1: -
package net.test.etest.dto;
import java.io.Serializable;
import java.util.List;
import org.apache.commons.lang.builder.ToStringBuilder;
import com.thoughtworks.xstream.annotations.XStreamAlias;
@XStreamAlias("POLICYDMO")
public class PolicyDTO implements Serializable {
/**
*
*/
private static final long serialVersionUID = 5293821325394812329L;
@XStreamAlias("POLICYKEY")
private long policyKey;
@XStreamAlias("ADDRESSES")
private List<AddressDTO> addresses;
@XStreamAlias("POLICY_INFO")
private PolicyInfoDTO policyInfo;
@XStreamAlias("ELIGIBILITIES")
private List<EligibilityDTO> eligibility;
@XStreamAlias("POLICY_AGENCY")
private PolicyAgencyDTO policyAgency;
@XStreamAlias("PARTICIPANTS")
private List<ParticipantDTO> participants;
@XStreamAlias("VEHICLES")
private List<VehicleDTO> vehicles;
@XStreamAlias("PREFILLEDVEHICLES")
private List<VehicleDTO> prefilledVehicles;
@XStreamAlias("DRIVERS")
private List<DriverDTO> drivers;
@XStreamAlias("PRIOR_CARRIER")
private PriorCarrierDTO priorCarrier;
@XStreamAlias("DISCOUNTS")
private List<DiscountDTO> discounts;
@XStreamAlias("COMPANIONPOLICY")
private CompanionPolicyDTO companionPolicy;
@XStreamAlias("PAYMENT")
private PaymentDTO payment;
// @XStreamAlias("BILLING")
//private BillingDTO billing;
@XStreamAlias("PAYMENT_PLAN")
private PaymentPlanDTO paymentPlan;
@XStreamAlias("POLICYTHIRDPARTYSTATUSES")
private List<PolicyThirdPartyDTO> policyThirdParty;
@XStreamAlias("UW_RULE_RESULTS")
private List<UWRuleResultDTO> uwRuleResults;
private String violationsCallStatus;
private String claimsCallStatus;
public PriorCarrierDTO getPriorCarrier() {
return priorCarrier;
}
public void setPriorCarrier(PriorCarrierDTO priorCarrier) {
this.priorCarrier = priorCarrier;
}
public long getPolicyKey() {
return policyKey;
}
public List<AddressDTO> getAddresses() {
return addresses;
}
public PolicyInfoDTO getPolicyInfo() {
return policyInfo;
}
public PaymentDTO getPayment() {
return payment;
}
public void setPayment(PaymentDTO payment) {
this.payment = payment;
}
public List<ParticipantDTO> getParticipants() {
return participants;
}
public void setPolicyKey(long policyKey) {
this.policyKey = policyKey;
}
public void setAddresses(List<AddressDTO> addresses) {
this.addresses = addresses;
}
public void setPolicyInfo(PolicyInfoDTO policyInfo) {
this.policyInfo = policyInfo;
}
public void setParticipants(List<ParticipantDTO> participants) {
this.participants = participants;
}
public List<VehicleDTO> getVehicles() {
return vehicles;
}
public void setVehicles(List<VehicleDTO> vehicles) {
this.vehicles = vehicles;
}
public List<DriverDTO> getDrivers() {
return drivers;
}
public void setDrivers(List<DriverDTO> drivers) {
this.drivers = drivers;
}
public List<DiscountDTO> getDiscounts() {
return discounts;
}
public void setDiscounts(List<DiscountDTO> discounts) {
this.discounts = discounts;
}
public CompanionPolicyDTO getCompanionPolicy() {
return companionPolicy;
}
public void setCompanionPolicy(CompanionPolicyDTO companionPolicy) {
this.companionPolicy = companionPolicy;
}
public PolicyAgencyDTO getPolicyAgency() {
return policyAgency;
}
public void setPolicyAgency(PolicyAgencyDTO policyAgency) {
this.policyAgency = policyAgency;
}
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
public List<EligibilityDTO> getEligibility() {
return eligibility;
}
public void setEligibility(List<EligibilityDTO> eligibility) {
this.eligibility = eligibility;
}
public String getEligibilityAnswerForQuestion(String question) {
if(eligibility!=null) {
for(EligibilityDTO e : eligibility) {
if(e.getQuestion().equals(question)) {
return e.getAnswer();
}
}
}
return null;
}
public List<PolicyThirdPartyDTO> getPolicyThirdParty() {
return policyThirdParty;
}
public void setPolicyThirdParty(List<PolicyThirdPartyDTO> policyThirdParty) {
this.policyThirdParty = policyThirdParty;
}
public List<UWRuleResultDTO> getUwRuleResults() {
return uwRuleResults;
}
public void setUwRuleResults(List<UWRuleResultDTO> uwRuleResults) {
this.uwRuleResults = uwRuleResults;
}
public PaymentPlanDTO getPaymentPlan() {
return paymentPlan;
}
public void setPaymentPlan(PaymentPlanDTO paymentPlan) {
this.paymentPlan = paymentPlan;
}
/*public BillingDTO getBilling() {
return billing;
}
public void setBilling(BillingDTO billing) {
this.billing = billing;
}*/
public List<VehicleDTO> getPrefilledVehicles() {
return prefilledVehicles;
}
public void setPrefilledVehicles(List<VehicleDTO> prefilledVehicles) {
this.prefilledVehicles = prefilledVehicles;
}
public String getViolationsCallStatus() {
return violationsCallStatus;
}
public void setViolationsCallStatus(String violationsCallStatus) {
this.violationsCallStatus = violationsCallStatus;
}
public String getClaimsCallStatus() {
return claimsCallStatus;
}
public void setClaimsCallStatus(String claimsCallStatus) {
this.claimsCallStatus = claimsCallStatus;
}
}
Bean2: -
package net.test.etest.services.persistence;
import java.util.ArrayList;
import java.util.List;
import net.test.etest.services.util.IBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
@JsonDeserialize(builder = PolicyDMO.Builder.class)
public class PolicyDMO {
private final long policyKey;
private final List<AddressDMO> addresses;
private final PolicyInfoDMO policyInfo;
private final List<EligibilityDMO> eligibility;
private final PolicyAgencyDMO policyAgency;
private final List<ParticipantDMO> participants;
private final List<DriverDMO> drivers;
private final PriorCarrier priorCarrier;
private final List<VehicleDMO> vehicles;
private final List<VehicleDMO> prefilledVehicles;
private final List<DiscountDMO> discounts;
private final CompanionPolicyDMO companionPolicy;
private final PaymentDMO payment;
private final List<PolicyThirdPartyDMO> policyThirdParty;
private final List<UWRuleResultDMO> uwRuleResults;
private final String violationsCallStatus;
private final String claimsCallStatus;
private final String frOrderStatus;
public String getViolationsCallStatus() {
return violationsCallStatus;
}
public String getClaimsCallStatus() {
return claimsCallStatus;
}
public long getPolicyKey() {
return policyKey;
}
public List<AddressDMO> getAddresses() {
return addresses;
}
public PolicyInfoDMO getPolicyInfo() {
return policyInfo;
}
public List<ParticipantDMO> getParticipants() {
return participants;
}
public List<DriverDMO> getDrivers() {
return drivers;
}
public List<VehicleDMO> getVehicles() {
return vehicles;
}
public PriorCarrier getPriorCarrier() {
return priorCarrier;
}
public List<DiscountDMO> getDiscounts() {
return discounts;
}
public CompanionPolicyDMO getCompanionPolicy() {
return companionPolicy;
}
public PaymentDMO getPayment() {
return payment;
}
private PolicyDMO(Builder builder) {
this.policyKey = builder.policyKey;
this.addresses = (builder.addresses != null) ? builder.addresses : builder.addresses;
this.policyInfo = builder.policyInfo;
this.policyAgency = builder.policyAgency;
this.participants = (builder.participants != null) ? builder.participants : builder.participants;
this.drivers = (builder.drivers != null) ? builder.drivers : builder.drivers;
this.vehicles = builder.vehicles;
this.prefilledVehicles = builder.prefilledVehicles;
this.priorCarrier = builder.priorCarrier;
this.discounts = builder.discounts;
this.companionPolicy = builder.companionPolicy;
this.payment = builder.payment;
this.eligibility = builder.eligibility;
this.policyThirdParty = builder.policyThirdParty;
this.uwRuleResults = builder.uwRuleResults;
this.violationsCallStatus = builder.violationsCallStatus;
this.claimsCallStatus = builder.claimsCallStatus;
this.frOrderStatus = builder.frOrderStatus;
}
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Builder implements IBuilder<PolicyDMO> {
private long policyKey;
/*
* private String company; private String state; private String lob;
* private String channel;
*/
private List<AddressDMO> addresses;
private PolicyInfoDMO policyInfo;
private PolicyAgencyDMO policyAgency;
private List<ParticipantDMO> participants;
private List<DriverDMO> drivers;
private List<VehicleDMO> vehicles;
private List<VehicleDMO> prefilledVehicles;
private PriorCarrier priorCarrier;
private List<DiscountDMO> discounts;
private CompanionPolicyDMO companionPolicy;
private PaymentDMO payment;
private List<EligibilityDMO> eligibility;
private List<PolicyThirdPartyDMO> policyThirdParty;
private List<UWRuleResultDMO> uwRuleResults;
private String violationsCallStatus;
private String claimsCallStatus;
private String frOrderStatus;
public Builder withDrivers(List<DriverDMO> drivers) {
this.drivers = drivers;
return this;
}
public Builder withPriorCarrier(PriorCarrier priorCarrier) {
this.priorCarrier = priorCarrier;
return this;
}
public Builder withVehicles(List<VehicleDMO> vehicles) {
this.vehicles = vehicles;
return this;
}
public Builder withPrefilledVehicles(List<VehicleDMO> prefilledVehicles) {
this.prefilledVehicles = prefilledVehicles;
return this;
}
public Builder withPolicyKey(long policyKey) {
this.policyKey = policyKey;
return this;
}
public Builder withAddresses(List<AddressDMO> addresses) {
this.addresses = addresses;
return this;
}
public Builder withPolicyInfo(PolicyInfoDMO policyInfo) {
this.policyInfo = policyInfo;
return this;
}
public Builder withPolicyAgency(PolicyAgencyDMO policyAgency) {
this.policyAgency = policyAgency;
return this;
}
public Builder withParticipants(List<ParticipantDMO> participants) {
this.participants = participants;
return this;
}
public Builder withDiscounts(List<DiscountDMO> discounts) {
this.discounts = discounts;
return this;
}
public Builder withCompanionPolicy(CompanionPolicyDMO companionPolicy) {
this.companionPolicy = companionPolicy;
return this;
}
public Builder withPayment(PaymentDMO payment) {
this.payment = payment;
return this;
}
public Builder withViolationsCallStatus(String b) {
this.violationsCallStatus = b;
return this;
}
public Builder withClaimsCallStatus(String b) {
this.claimsCallStatus = b;
return this;
}
public Builder withUWRuleResults(List<UWRuleResultDMO> uwRuleResults) {
this.uwRuleResults = uwRuleResults;
return this;
}
public Builder(PolicyDMO policyDMO) {
this.policyKey = policyDMO.policyKey;
this.addresses = policyDMO.addresses;
this.policyInfo = policyDMO.policyInfo;
this.policyAgency = policyDMO.policyAgency;
this.participants = policyDMO.participants;
this.drivers = policyDMO.drivers;
this.vehicles = policyDMO.vehicles;
this.prefilledVehicles = policyDMO.prefilledVehicles;
this.priorCarrier = policyDMO.priorCarrier;
this.discounts = policyDMO.discounts;
this.companionPolicy = policyDMO.companionPolicy;
this.payment = policyDMO.payment;
this.eligibility = policyDMO.eligibility;
this.policyThirdParty = policyDMO.policyThirdParty;
this.uwRuleResults = policyDMO.uwRuleResults;
this.violationsCallStatus = policyDMO.violationsCallStatus;
this.claimsCallStatus = policyDMO.violationsCallStatus;
this.frOrderStatus = policyDMO.frOrderStatus;
}
/* For Json deserialization */
public Builder() {
}
public Builder withEligibility(List<EligibilityDMO> eligibility) {
this.eligibility = eligibility;
return this;
}
public Builder withPolicyThirdParty(List<PolicyThirdPartyDMO> policyThirdParty) {
this.policyThirdParty = policyThirdParty;
return this;
}
public Builder withUwRuleResults(List<UWRuleResultDMO> uwRuleResults) {
this.uwRuleResults = uwRuleResults;
return this;
}
public Builder withFrOrderStatus(String frOrderStatus) {
this.frOrderStatus = frOrderStatus;
return this;
}
public PolicyDMO build() {
return new PolicyDMO(this);
}
}
public PolicyAgencyDMO getPolicyAgency() {
return policyAgency;
}
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
public List<EligibilityDMO> getEligibility() {
return eligibility;
}
public List<PolicyThirdPartyDMO> getPolicyThirdParty() {
if (policyThirdParty == null) {
return new ArrayList<PolicyThirdPartyDMO>();
}
return policyThirdParty;
}
public List<UWRuleResultDMO> getUwRuleResults() {
return uwRuleResults;
}
public List<VehicleDMO> getPrefilledVehicles() {
return prefilledVehicles;
}
public String getFrOrderStatus() {
return frOrderStatus;
}
}
答案 0 :(得分:1)
似乎没有任何明显更简单的方法将数据从简单的POJO复制到构建器模式bean。只有当getter和setter方法名称在两个bean中匹配时,BeanUtils.copyProperties才有效,但情况并非如此。
在构建器模式bean中添加setter方法将使其工作,但是bean不会是不可变的。它似乎没什么意义。
你可能不得不做那些复制。
答案 1 :(得分:1)
我通过将bean1转换为JSON而不是将JSON转换回Bean2来完成解决方法。这是代码。
public static PolicyDMO createPolicyDMO(PolicyDTO policydto){
PolicyDMO policyDMO = null;
if (policydto != null) {
try {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
policyDMO = mapper.readValue(mapper.writeValueAsString(policydto), PolicyDMO.class);
} catch (JsonGenerationException e)
{
e.printStackTrace();
} catch (JsonMappingException e)
{
e.printStackTrace();
} catch (Exception e) {
}
} else {
LOGGER.error("unable to parse the object that is NULL");
}
return policyDMO;
}