java - Collection<?> 将元素包装在 JAXB 中实际类型的复数名称中

标签 java xml jaxb eclipselink moxy

我有一个Response包含一些基本属性和通配符的类 Collection<?> .

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Response {
    private String approved;
    private String errorCode;
    @XmlAnyElement(lax = true)
    private Collection<?> collection;

    public Response() {
    }

    public Response(String approved, Collection<?> collection) {
        this.approved = approved;
        this.collection = collection;
    }

    public String getApproved() {
        return approved;
    }

    public String getErrorCode() {
        return errorCode;
    }

    public Collection<?> getCollection() {
        return collection;
    }
}

这个集合可以包含多种类型,例如这个类型:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Transaction {
    private BigDecimal amount;
    private String transactionId;

    public Transaction(BigDecimal amount, String transactionId ) {
        super();
        this.amount = amount;
        this.transactionId = transactionId ;
    }

    public Transaction() {
        super();
    }

    public BigDecimal getAmount() {
        return amount;
    }

    public String getTransactionId() {
        return transactionId;
    }

    public void setAmount(BigDecimal amount) {
        this.amount = amount;
    }

    public void setTransactionId(String transactionId) {
        this.transactionId = transactionId;
    }
}

序列化 Response 时类,我得到这个 XML。

<?xml version="1.0" encoding="UTF-8"?>
<response>
   <approved>00</approved>
   <errorCode></errorCode>
   <transaction>
      <amount>500.00</amount>
      <transactionId>pgka3902</transactionId>
   </transaction>
   <transaction>
      <amount>201.05</amount>
      <transactionId>abcd3020</transactionId>
   </transaction>
</response>

添加@XmlElementWrapper包裹<transaction> <collection> 中的元素这仍然是 Not Acceptable 。 我需要将包装器命名为集合中实际类型的复数。例如,上面的xml应该是:

<?xml version="1.0" encoding="UTF-8"?>
<response>
   <approved>00</approved>
   <errorCode />
   <transactions>
      <transaction>
         <amount>500.00</amount>
         <transactionId>pgka3902</transactionId>
      </transaction>
      <transaction>
         <amount>201.05</amount>
         <transactionId>abcd3020</transactionId>
      </transaction>
   </transactions>
</response>

可以用 JAXB 来做到这一点吗?我正在使用 Eclipselink Moxy 实现。

最佳答案

您可以将其更改为Object,而不是使用Response 保存Collection。然后,您可以为每种集合类型设置不同的类。

 @XmlRootElement
 @XmlAccessorType(XmlAccessType.FIELD)
 public class Transactions {

     @XmlElement(name="transaction")
     private List<Transaction> transactions;

 }

关于java - Collection<?> 将元素包装在 JAXB 中实际类型的复数名称中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26844411/

相关文章:

python - 如何在python中获取两个xml标签之间的内容?

python - 从 Python : Python equivalent of . NET XmlTextWriter 写入 XML?

rest - JAXB Jackson - 如何处理 map ?

java - 动态根元素 JAXB?

java - JAXB Marshaller 没有值为 null 的元素

java - 我的 java 程序中有多少个线程正在运行?

java - 用于 Java 的 Rest 客户端生成器

xml - BPEL错误: [MessageVariableRequired] Cannot use non-message variable

java - 我们可以使用 Web 驱动程序来测试 https ://sites? 我正在尝试在 gmail.com 上工作,但它不起作用

JAVA 如何打印一个包含3个整数值的对象?