例如,我有一个 POJO,如下所示,但它提供给多个操作,但我不想创建几个相同的 POJO,因为每个操作的根元素名称都会更改。因此我需要一个 POJO,但我可以动态更改根元素名称。
@ToString
@MappedSuperclass
@lombok.Data
@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
@EqualsAndHashCode(callSuper = false)
public class AmountOrAccountBlockOrUnblockRequest extends XmlBuilder implements SessionGenerator {
@JsonIgnore
@XmlElement
private String TargetBankVerificationNumber;
@JsonIgnore
@XmlElement
private String Narration;
@JsonProperty("amount")
@XmlElement(name = "Amount")
private String amount;
@JsonProperty("savingAccountNumber")
@XmlElement(name = "TargetAccountNumber")
private String targetAccountNumber;
@JsonIgnore
@XmlElement
private String ChannelCode;
@JsonProperty("unblockId")
@JsonIgnore
@XmlElement
private String ReferenceCode;
@JsonIgnore
@XmlElement
private String DestinationInstitutionCode;
@JsonIgnore
@XmlElement
private String TargetAccountName;
@XmlElement
private String SessionID;
@JsonIgnore
@XmlElement
private String ReasonCode;
// if account is still blocked or released
@JsonProperty("block")
private boolean blockUnblock;
@JsonProperty("blockUnblockReason")
private String blockUnblockReason;
@Override
public String toXmlString() {
return super.convertObjectToXmlString(this, this.getClass());
}
@Override
public void generateSessionID(HelperFacade helperFacade) {
setSessionID(helperFacade.generateSessionID(this.getDestinationInstitutionCode()));
}
}
上面的这个 POJO 将服务于多个操作,但每个操作具有不同的根元素名称,例如,
<AmountUnblockRequest>
<SessionID>000001100913103301000000000001</SessionID>
<DestinationInstitutionCode>000002</DestinationInstitutionCode>
<ChannelCode>7</ChannelCode>
<ReferenceCode>xxxxxxxxxxxxxxx</ReferenceCode>
<TargetAccountName>Ajibade Oluwasegun</TargetAccountName>
<TargetBankVerificationNumber>1033000442</TargetBankVerificationNumber>
<TargetAccountNumber>2222002345</TargetAccountNumber>
<ReasonCode>0001</ReasonCode>
<Narration>Transfer from 000002 to 0YY</Narration>
<Amount>1000.00</Amount>
</AmountUnblockRequest>
和
<AmountBlockRequest>
<SessionID>000001100913103301000000000001</SessionID>
<DestinationInstitutionCode>000002</DestinationInstitutionCode>
<ChannelCode>7</ChannelCode>
<ReferenceCode>xxxxxxxxxxxxxxx</ReferenceCode>
<TargetAccountName>Ajibade Oluwasegun</TargetAccountName>
<TargetBankVerificationNumber>1033000442</TargetBankVerificationNumber>
<TargetAccountNumber>2222002345</TargetAccountNumber>
<ReasonCode>0001</ReasonCode>
<Narration>Transfer from 000002 to 0YY</Narration>
<Amount>1000.00</Amount>
</AmountBlockRequest>
我想避免必须创建两个相同的类的痛苦,因为根元素名称会改变。