views:

1982

answers:

4

I am Developing a Web Service using WCF.It is an interoperable Web Service.Now i am consuming this Web Service from a Java Client.now when i created the proxy class.It created all getter & setter method.now in that proxy class it created a JAXBElement field.I searched for it in jdk api for it.and found constructor.

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

I am finding it hard to use this constructor.if anyone can please show me how to use this constructor.please explain all the parameter and if there is a good tutorial on net for using it.

+1  A: 

The first parameter name is a qualified name (local name + name space) of the XML element you are mirroring.

declaredType is class object of the class you are binding your XML element to.

scope is usage scope of the XML Element, but you can set it to null.

value is an instance of declaredType class i.e. the actual Java object bound to actual XML element instance.

Superfilin
A: 

So Where is the usage.. and Example. I wanna it..

I found it. so you maybe do that.. it will work.

Text t = new Text(); t.setValue("I need limo transportation"); JAXBElement text = new JAXBElement(SERVICE, Text.class, t);

question
A: 

I stumbled upon this question while I was looking for the same answer. I posted an answer but found a few problems. Here is a way to do it:

new javax.xml.bind.JAXBElement(
        new javax.xml.namespace.QName("http://locationOfURI", "nameOfElement"),
        javax.xml.bind.JAXBElement.class, 
        null, what your object's value is );

The last means the type paramter of JAXBElement.

Hope this works.

Tesnep
A: 

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"&gt; 
<jaxb:bindings schemaLocation="http://localhost:15002/MiddlewareDataServiceConversion?xsd=xsd0"&gt; 
    <jaxb:bindings node="/xs:schema"> 
        <jaxb:globalBindings generateElementProperty="false"/> 
    </jaxb:bindings> 
</jaxb:bindings> 

Luis Rosa