Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use JAXBElement<String> in Web Service?

I am developing an interoperable web service using WCF which I am consuming from a Java client. When I created the proxy class it generated all the getter and setter methods as well as a JAXBElement<String> field. I searched for this in the JDK API and found the constructor:

JAXBElement(QName name, Class<T> declaredType, Class scope, T value)  

How should I use this constructor? Please explain the parameters and let me know if there is a good tutorial on the Internet describing its use.

like image 588
Chintan Shah Avatar asked Sep 08 '09 13:09

Chintan Shah


People also ask

How do you declare a JAXBElement of a string?

You can do the following: JAXBElement<String> jaxbElement = new JAXBElement(new QName("http://mycompany/services", "amount"), String. class, "Hello World"); There should also be a create method on the generated ObjectFactory class that will create this instance of JAXBElement with the appropriate info for you.

How do I get data from JAXBElement?

1 Answer. Show activity on this post. If getProject() returns the type JAXBElement<String> then getName() returns the name of the XML tag. To get the value of that element you need to call getValue() .

What is the use of JAXBElement?

JAXBElement is used to preserve the element name/namespace in use cases where enough information is not present in the object model. It's often used with substitution groups. Without any JAXB metada the result will be wrapped in a JAXBElement.

What is QName in JAXBElement?

QName getName() This method returns the xml element tag name. 3. Class getScope() This method returns scope of xml element declaration.


1 Answers

A solution this problem is, you do not need to create a seperate constructor for creating a JAXBElement. The respected element can be retrieved from objectFactory.create........() method. Suppose you want to create and set some value in response object, and argument is as of JAXBElement type, then you need to do this way:

someResponseObj.setMyValue(objectFactory.create.......());  /*method name that will be return a JAXBElement in setter()*/ 

Note: Please check the ObjectFactory reference because there can be multiple ObjectFactory classes in generated code so you need to refer the exact one which is associated to the class of that package.

like image 154
Mukul Kumar Avatar answered Oct 16 '22 20:10

Mukul Kumar