How do I instantiate a JAXBElement<String> object?

JavaJaxb

Java Problem Overview


I need to create one of these as the interface requires it. Can someone please let me know how to create one, as there doesn't seem to be a c'tor defined?

Java Solutions


Solution 1 - Java

When you imported the WSDL, you should have an ObjectFactory class which should have bunch of methods for creating various input parameters.

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

Solution 2 - Java

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

Solution 3 - Java

Here is how I do it. You will need to get the namespace URL and the element name from your generated code.

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

Solution 4 - Java

Other alternative:

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

Then:

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

Solution 5 - Java

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmiguelView Question on Stackoverflow
Solution 1 - JavaGauravView Answer on Stackoverflow
Solution 2 - JavavikView Answer on Stackoverflow
Solution 3 - JavaJC.View Answer on Stackoverflow
Solution 4 - JavaalditisView Answer on Stackoverflow
Solution 5 - JavaMatthew FlaschenView Answer on Stackoverflow