How to use JAXBElement<String> in Web Service? How to use JAXBElement<String> in Web Service? xml xml

How to use JAXBElement<String> in Web Service?


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.


Just in case someone ends up here looking for a solution: Instead of using JAXBElement one can use only the Type by setting the generateElementProperty to false in a bindings file.

In my case Im using maven to generate the stub files from wsdl.

partial pom file and bindings file (in this config is called javabindings.xml)

    <plugin>        <groupId>org.apache.cxf</groupId>        <artifactId>cxf-codegen-plugin</artifactId>        <version>2.2.9</version>        <executions>        <execution>            <id>generate-sources</id>            <phase>generate-sources</phase>            <configuration>            <sourceRoot>${project.build.directory}/wsdl2java/generated-sources/src/main/java</sourceRoot>            <wsdlOptions>                <wsdlOption>                    <wsdl>${basedir}/src/main/resources/yourWsdlFileOrURL.wsdl</wsdl>                    <extraargs>                        <extraarg>-verbose</extraarg>                        <extraarg>-b</extraarg>                        <extraarg>${basedir}/src/main/resources/javabindings.xml</extraarg>                    </extraargs>                                        </wsdlOption>            </wsdlOptions>            </configuration>            <goals>                <goal>wsdl2java</goal>            </goals>        </execution>        </executions>    </plugin>
<jaxb:bindings version="2.0" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <jaxb:bindings schemaLocation="http://localhost:15002/MiddlewareDataServiceConversion?xsd=xsd0">     <jaxb:bindings node="/xs:schema">         <jaxb:globalBindings generateElementProperty="false"/>     </jaxb:bindings> </jaxb:bindings> 


I'm just doing the exact same thing, using a Java Client to comsume WCF web service. I'm using the jaxws maven plugin to generate the client side code.

<plugin>    <groupId>org.codehaus.mojo</groupId>    <artifactId>jaxws-maven-plugin</artifactId>    <configuration>        <packageName>com.package1</packageName>        <sourceDestDir>src/main/java</sourceDestDir>        <wsdlDirectory>src/main/resources</wsdlDirectory>        <wsdlFiles>            <wsdlFile>wcf_webservice.wsdl</wsdlFile>        </wsdlFiles>    </configuration></plugin>

You should have an ObjectFactory class in your com.package1 which you can use to create your JAXBElements for you eg:

ObjectFactory factory = new ObjectFactory();MyObject myObject = new MyObject();JAXBElement<MyObject> elem = factory.createMyObject(myObject);

Just as a side note, you'll need to flatten the wcf wsdl before jaxws can use it.