gpt4 book ai didi

java - 如何在 List> 中实例化和存储值

转载 作者:行者123 更新时间:2023-11-30 12:03:39 25 4
gpt4 key购买 nike

我已经从 Wsdl 生成了 java 客户端。我被困在下面的代码中,我必须在下面的 List<JAXBElement<?>> 中设置一些 setter 值

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> ETC,但是我怎样才能将值存储在 JAXBElement 中呢?类型 <?>然后将其存储在列表中?

更新:

借助这个 [ https://stackoverflow.com/a/19548424/9811170] ,我找到了 ObjectFacotry 类,其中包含需要设置的值的创建函数。

JAXBElement<?> jasbElem = null;     
jasbElem = objectFactory.createSampleVerificationDomainCNIC("2392923");
jasbElem = objectFactory.createSampleVerificationDomainMSISDN("xxxxxxx");
jasbElem = objectFactory.createSampleVerificationDomainMsg("some message");
jasbElem = objectFactory.createSampleVerificationDomainUserName("apiusername");
jasbElem = objectFactory.createSampleVerificationDomainPassword("testpass");


SampleTestVerificationDomain.getRest().add(jasbElem);

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

最佳答案

你能检查一下吗: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);

关于java - 如何在 List<JAXBElement<?>> 中实例化和存储值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57560490/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com