java - 一个 POJO 但 XmlRootElement 名称不同

标签 java xml pojo xmlroot

例如,我有一个 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>

我想避免必须创建两个相同的类的痛苦,因为根元素名称将会改变。

最佳答案

您可以使用Declarative Stream Mapping (DSM)流解析库。您不需要同时为 XML 和 JSON 注释 POJO。

您只需定义要从 XML 中提取的数据的映射即可。

以下是 XML 的映射定义。

result:
  path: /(AmountBlockRequest|AmountUnblockRequest) // path is regex
  type: object
  fields:
    TargetBankVerificationNumber:
    Narration:
    amount:
      path: amount
      xml:
        path: Amount
    targetAccountNumber:
      path: targetAccountNumber
      xml:
        path: TargetAccountNumber

    channelCode:
      path: ChannelCode
    referenceCode:
      path: ReferenceCode
    destinationInstitutionCode:
      path: DestinationInstitutionCode
    targetAccountName:
      path: TargetAccountName
    sessionID:
      path: SessionID
    reasonCode:
      path: ReasonCode
    blockUnblockReason:
      path: BlockUnblockReason

解析 XML 的 Java 代码:

DSM dsm=new DSMBuilder(new File("path/to/mapping.yaml")).setType(DSMBuilder.TYPE.XML)..create(AmountOrAccountBlockOrUnblockRequest.class);;
// For json
//DSM dsm=new DSMBuilder(new File("path/to/mapping.yaml")).setType(DSMBuilder.TYPE.JSON)..create(AmountOrAccountBlockOrUnblockRequest.class);
AmountOrAccountBlockOrUnblockRequest result=  (AmountOrAccountBlockOrUnblockRequest)dsm.toObject(xmlFileContent);
// json represntation fo result
dsm.getObjectMapper().writerWithDefaultPrettyPrinter().writeValue(System.out, object);

这是输出:

   {
  "amount" : "1000.00",
  "targetAccountNumber" : "2222002345",
  "blockUnblock" : false,
  "blockUnblockReason" : null,
  "sessionID" : "000001100913103301000000000001",
  "reasonCode" : "0001",
  "referenceCode" : "xxxxxxxxxxxxxxx",
  "channelCode" : "7",
  "narration" : null,
  "targetBankVerificationNumber" : null,
  "destinationInstitutionCode" : "000002",
  "targetAccountName" : "Ajibade Oluwasegun"
}

目前不支持序列化。

关于java - 一个 POJO 但 XmlRootElement 名称不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60991205/

相关文章:

java - 需要帮助理解有关并行流性能提升的文章

java - 为什么对象在单例模式中是静态的?

java - 正在添加空默认 XML 命名空间 xmlns =""属性?

android - onActivityResult 在 Activity 范围之外

java - Jackson 映射到 POJO 无法反序列化实例实例

java - 是否有在运行时从 Pojo 动态生成 Swing GUI 的框架?

JAVA - 解析巨大(超大)JSON 文件的最佳方法

android - 在Android中,在OnClick中显示按下的按钮动画?

php - 使用 php 发送 xml

java - 此 Java Scanner 示例中的正确分隔符是什么?