如何实例化和存储List <JAXBElement <?>>中的值

时间:2019-08-19 16:15:28

标签: java list soap jaxbelement

我已经从Wsdl生成了Java客户端。我被困在以下代码中,我必须在以下List<JAXBElement<?>>

中设置一些设置器值
public class SampleVerificationDomain
       extends BaseDomain
    {

       protected List<JAXBElement<?>> rest;

       /**
         * Gets the rest of the content model. 
         *  
         * <p>
         * This accessor method returns a reference to the live list,
         * not a snapshot. Therefore any modification you make to the
         * returned list will be present inside the JAXB object.
         * This is why there is not a <CODE>set</CODE> method for the rest property.
         * 
         * <p>
         * For example, to add a new item, do as follows:
         * <pre>
         *    getRest().add(newItem);
         * </pre>
         *  
         * <p>
         * Objects of the following type(s) are allowed in the list
         * {@link JAXBElement }{@code <}{@link String }{@code >}
         * {@link JAXBElement }{@code <}{@link String }{@code >}
         * {@link JAXBElement }{@code <}{@link String }{@code >}
         * {@link JAXBElement }{@code <}{@link String }{@code >}
         * {@link JAXBElement }{@code <}{@link DummyVerification }{@code >}
         * {@link JAXBElement }{@code <}{@link String }{@code >}
         *
         */
        public List<JAXBElement<?>> getRest() {
            if (rest == null) {
                rest = new ArrayList<JAXBElement<?>>();
            }
            return this.rest;
        }

如果它是List<Class>List<String>等,我知道如何存储值, 但是如何将值存储在类型为JAXBElement的{​​{1}}中,然后将其存储在列表中?

更新:

借助此[https://stackoverflow.com/a/19548424/9811170],我发现了ObjectFacotry类,该类包含需要设置的值的创建函数。

<?>

但是上面的代码仅设置JAXBElement中的最后一个值。 关于如何在JAXBElement中存储所有值的任何帮助吗?

1 个答案:

答案 0 :(得分:0)

您可以检查以下内容:https://dzone.com/articles/jaxb-and-root-elements

JAXBContext jc = JAXBContext.newInstance("org.example.customer");
    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    // Create Domain Objects
    AddressType billingAddress = new AddressType();
    billingAddress.setStreet("1 Any Street");
    Customer customer = new Customer();
    customer.setBillingAddress(billingAddress);

    // Marshal Customer
    marshaller.marshal(customer, System.out);

    // Marshal Billing Address
    ObjectFactory objectFactory = new ObjectFactory();
    JAXBElement<AddressType> je =  objectFactory.createBillingAddress(billingAddress);
    marshaller.marshal(je, System.out);