如何实例化JAXBElement <String>对象?


120

我需要创建其中之一,因为接口需要它...有人可以让我知道如何创建它,因为似乎没有定义吗?

Answers:


179

导入WSDL时,应该有一个ObjectFactory类,该类应具有用于创建各种输入参数的方法。

ObjectFactory factory = new ObjectFactory();
JAXBElement<String> createMessageDescription = factory.createMessageDescription("description");
message.setDescription(createMessageDescription);

7
ObjectFactory fact = new ObjectFactory();   
JAXBElement<String> str = fact.createCompositeTypeStringValue("vik");    
comp.setStringValue(str);
CompositeType retcomp = service.getDataUsingDataContract(comp);
System.out.println(retcomp.getStringValue().getValue());

7

这是我的方法。您将需要从生成的代码中获取名称空间URL和元素名称。

new JAXBElement(new QName("http://www.novell.com/role/service","userDN"),
                new String("").getClass(),testDN);

6
最好使用如下所述的ObjectFactory类,而不是使用JAXBElement构造函数
Harish

3

其他选择:

JAXBElement<String> element = new JAXBElement<>(new QName("Your localPart"),
                                                String.class, "Your message");

然后:

System.out.println(element.getValue()); // Result: Your message

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.