users@jaxb.java.net

Easy JAXBElement ? from a complete Neophite (aka Newbie)

From: RichardCromer <richard.cromer_at_itt.com>
Date: Tue, 11 Dec 2007 07:36:58 -0800 (PST)

Greetings All,

I am a newbie at JAXB, web services, and the rest of it, so I have what I am
sure is an easy question for someone with even a moderate amount of
knowledge about this.

Usings Netbeans 5.5 I developed a client for a web service I am developing.
The client and web service is working fine, and I am able, with this client
to pass and recieve value data, that is ints, floats, strings, etc. Now I
am dealing with a member variable, mParentID among others, that Netbeans
defines as a JAXBElement (actually on the server side these variables where
declared as pointers in the containing class).

With that said, I do not really know how to construct the mParentID variable
as a JAXBElement. I have been looking at different docs but I have not
found (or rather recognized) a reasonable example. In addition, if anyone
might happen to know of a good tutorial site (that really attempts to
explains some of this stuff) I would appreciate that too.

The shape class code is lengthy, but if you could just show me how to create
a JAXBElement for mParentID, I am sure I could do it for the rest. (I am
including the full class to make sure I didn't neglect to share anything
that is useful.)

2 Code follows, the Shape Class followed by my main program (each headlined
by 6 askerisks + message)

****** First is the Class, called Shape, that is generated by NetBeans
(Notice the protected element mParentID).

package documentliteralclient;

import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlType;


/**
 * <p>Java class for Shape complex type.
 *
 * <p>The following schema fragment specifies the expected content contained
within this class.
 *
 * <pre>
 * &lt;complexType name="Shape">
 * &lt;complexContent>
 * &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 * &lt;sequence>
 * &lt;element name="mID"
type="{http://www.w3.org/2001/XMLSchema}int"/>
 * &lt;element name="mParentID"
type="{http://www.w3.org/2001/XMLSchema}int" minOccurs="0"/>
 * &lt;element name="mChildrenIDs"
type="{http://www.w3.org/2001/XMLSchema}int" maxOccurs="unbounded"
minOccurs="0"/>
 * &lt;element name="mAperture"
type="{http://www.w3.org/2001/XMLSchema}boolean"/>
 * &lt;element name="mCombiner"
type="{http://www.w3.org/2001/XMLSchema}boolean"/>
 * &lt;element name="mName"
type="{http://www.w3.org/2001/XMLSchema}string"/>
 * &lt;element name="mUnit"
type="{http://www.w3.org/2001/XMLSchema}string"/>
 * &lt;element name="mAffectOnLight"
type="{urn:POCShapeWebService}LightAffect" minOccurs="0"/>
 * &lt;element name="mFrameRelativeToParent"
type="{urn:POCShapeWebService}CoordinateFrame" minOccurs="0"/>
 * &lt;element name="mObjectInLocalFrame"
type="{urn:POCShapeWebService}CoordinateFrame" minOccurs="0"/>
 * &lt;element name="mError"
type="{urn:POCShapeWebService}WaveFrontError" minOccurs="0"/>
 * &lt;/sequence>
 * &lt;/restriction>
 * &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 *
 *
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Shape", propOrder = {
    "mid",
    "mParentID",
    "mChildrenIDs",
    "mAperture",
    "mCombiner",
    "mName",
    "mUnit",
    "mAffectOnLight",
    "mFrameRelativeToParent",
    "mObjectInLocalFrame",
    "mError"
})
public class Shape {

    @XmlElement(name = "mID", namespace = "urn:POCShapeWebService")
    protected int mid;
    @XmlElementRef(name = "mParentID", namespace = "urn:POCShapeWebService",
type = JAXBElement.class)
    protected JAXBElement<Integer> mParentID;
    @XmlElement(namespace = "urn:POCShapeWebService", type = Integer.class)
    protected List<Integer> mChildrenIDs;
    @XmlElement(namespace = "urn:POCShapeWebService")
    protected boolean mAperture;
    @XmlElement(namespace = "urn:POCShapeWebService")
    protected boolean mCombiner;
    @XmlElement(namespace = "urn:POCShapeWebService", required = true)
    protected String mName;
    @XmlElement(namespace = "urn:POCShapeWebService", required = true)
    protected String mUnit;
    @XmlElementRef(name = "mAffectOnLight", namespace =
"urn:POCShapeWebService", type = JAXBElement.class)
    protected JAXBElement<LightAffect> mAffectOnLight;
    @XmlElementRef(name = "mFrameRelativeToParent", namespace =
"urn:POCShapeWebService", type = JAXBElement.class)
    protected JAXBElement<CoordinateFrame> mFrameRelativeToParent;
    @XmlElementRef(name = "mObjectInLocalFrame", namespace =
"urn:POCShapeWebService", type = JAXBElement.class)
    protected JAXBElement<CoordinateFrame> mObjectInLocalFrame;
    @XmlElementRef(name = "mError", namespace = "urn:POCShapeWebService",
type = JAXBElement.class)
    protected JAXBElement<WaveFrontError> mError;

    /**
     * Gets the value of the mid property.
     *
     */
    public int getMID() {
        return mid;
    }

    /**
     * Sets the value of the mid property.
     *
     */
    public void setMID(int value) {
        this.mid = value;
    }

    /**
     * Gets the value of the mParentID property.
     *
     * @return
     * possible object is
     * {_at_link JAXBElement }{_at_code <}{_at_link Integer }{_at_code >}
     *
     */
    public JAXBElement<Integer> getMParentID() {
        return mParentID;
    }

    /**
     * Sets the value of the mParentID property.
     *
     * @param value
     * allowed object is
     * {_at_link JAXBElement }{_at_code <}{_at_link Integer }{_at_code >}
     *
     */
    public void setMParentID(JAXBElement<Integer> value) {
        this.mParentID = ((JAXBElement<Integer> ) value);
    }

    /**
     * Gets the value of the mChildrenIDs property.
     *
     * <p>
     * This accessor method returns a reference to the live list,
     * not a snapshot. Therefore any modification you make to the
     * returned list will be present inside the JAXB object.
     * This is why there is not a <CODE>set</CODE> method for the
mChildrenIDs property.
     *
     * <p>
     * For example, to add a new item, do as follows:
     * <pre>
     * getMChildrenIDs().add(newItem);
     * </pre>
     *
     *
     * <p>
     * Objects of the following type(s) are allowed in the list
     * {_at_link Integer }
     *
     *
     */
    public List<Integer> getMChildrenIDs() {
        if (mChildrenIDs == null) {
            mChildrenIDs = new ArrayList<Integer>();
        }
        return this.mChildrenIDs;
    }

    /**
     * Gets the value of the mAperture property.
     *
     */
    public boolean isMAperture() {
        return mAperture;
    }

    /**
     * Sets the value of the mAperture property.
     *
     */
    public void setMAperture(boolean value) {
        this.mAperture = value;
    }

    /**
     * Gets the value of the mCombiner property.
     *
     */
    public boolean isMCombiner() {
        return mCombiner;
    }

    /**
     * Sets the value of the mCombiner property.
     *
     */
    public void setMCombiner(boolean value) {
        this.mCombiner = value;
    }

    /**
     * Gets the value of the mName property.
     *
     * @return
     * possible object is
     * {_at_link String }
     *
     */
    public String getMName() {
        return mName;
    }

    /**
     * Sets the value of the mName property.
     *
     * @param value
     * allowed object is
     * {_at_link String }
     *
     */
    public void setMName(String value) {
        this.mName = value;
    }

    /**
     * Gets the value of the mUnit property.
     *
     * @return
     * possible object is
     * {_at_link String }
     *
     */
    public String getMUnit() {
        return mUnit;
    }

    /**
     * Sets the value of the mUnit property.
     *
     * @param value
     * allowed object is
     * {_at_link String }
     *
     */
    public void setMUnit(String value) {
        this.mUnit = value;
    }

    /**
     * Gets the value of the mAffectOnLight property.
     *
     * @return
     * possible object is
     * {_at_link JAXBElement }{_at_code <}{_at_link LightAffect }{_at_code >}
     *
     */
    public JAXBElement<LightAffect> getMAffectOnLight() {
        return mAffectOnLight;
    }

    /**
     * Sets the value of the mAffectOnLight property.
     *
     * @param value
     * allowed object is
     * {_at_link JAXBElement }{_at_code <}{_at_link LightAffect }{_at_code >}
     *
     */
    public void setMAffectOnLight(JAXBElement<LightAffect> value) {
        this.mAffectOnLight = ((JAXBElement<LightAffect> ) value);
    }

    /**
     * Gets the value of the mFrameRelativeToParent property.
     *
     * @return
     * possible object is
     * {_at_link JAXBElement }{_at_code <}{_at_link CoordinateFrame }{_at_code >}
     *
     */
    public JAXBElement<CoordinateFrame> getMFrameRelativeToParent() {
        return mFrameRelativeToParent;
    }

    /**
     * Sets the value of the mFrameRelativeToParent property.
     *
     * @param value
     * allowed object is
     * {_at_link JAXBElement }{_at_code <}{_at_link CoordinateFrame }{_at_code >}
     *
     */
    public void setMFrameRelativeToParent(JAXBElement<CoordinateFrame>
value) {
        this.mFrameRelativeToParent = ((JAXBElement<CoordinateFrame> )
value);
    }

    /**
     * Gets the value of the mObjectInLocalFrame property.
     *
     * @return
     * possible object is
     * {_at_link JAXBElement }{_at_code <}{_at_link CoordinateFrame }{_at_code >}
     *
     */
    public JAXBElement<CoordinateFrame> getMObjectInLocalFrame() {
        return mObjectInLocalFrame;
    }

    /**
     * Sets the value of the mObjectInLocalFrame property.
     *
     * @param value
     * allowed object is
     * {_at_link JAXBElement }{_at_code <}{_at_link CoordinateFrame }{_at_code >}
     *
     */
    public void setMObjectInLocalFrame(JAXBElement<CoordinateFrame> value) {
        this.mObjectInLocalFrame = ((JAXBElement<CoordinateFrame> ) value);
    }

    /**
     * Gets the value of the mError property.
     *
     * @return
     * possible object is
     * {_at_link JAXBElement }{_at_code <}{_at_link WaveFrontError }{_at_code >}
     *
     */
    public JAXBElement<WaveFrontError> getMError() {
        return mError;
    }

    /**
     * Sets the value of the mError property.
     *
     * @param value
     * allowed object is
     * {_at_link JAXBElement }{_at_code <}{_at_link WaveFrontError }{_at_code >}
     *
     */
    public void setMError(JAXBElement<WaveFrontError> value) {
        this.mError = ((JAXBElement<WaveFrontError> ) value);
    }

}


****** Next, this is my main program. Notice I created a new JAXBElement
(with syntax I basically copied from the WEB) but it gets ignored by the web
service.

/*
 * Main.java
 */

package documentliteralclient;

import java.lang.Integer;
import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;

/**
 *
 * @author cromer
 */
public class Main {
    
    /** Creates a new instance of Main */
    public Main() {
    }
    
    /**
     * @param args the command line arguments
     */
    
    public static void main(String[] args) {
        
        try { // Call Web Service Operation
            documentliteralclient.Service service = new
documentliteralclient.Service();
            documentliteralclient.ServicePortType port =
service.getService();
            // TODO initialize WS operation arguments here
            documentliteralclient.Circle shape = new
documentliteralclient.Circle();
            
            shape.setMID(3);
            shape.setMRadius(2.2);
            shape.setMName("Circle1");
            shape.setMUnit("meter");
            javax.xml.bind.JAXBElement<Integer> number = new JAXBElement(new
QName("root"),Integer.class,2);
            shape.setMParentID(number);
            shape.setMFrameRelativeToParent(null);
            shape.setMAffectOnLight(null);
            shape.setMError(null);
            shape.setMObjectInLocalFrame(null);
            port.addShape(shape);
            
        } catch (Exception ex) {
                // TODO handle custom exceptions here
        }
    }
}




-- 
View this message in context: http://www.nabble.com/Easy-JAXBElement---from-a-complete-Neophite-%28aka-Newbie%29-tp14276335p14276335.html
Sent from the java.net - jaxb users mailing list archive at Nabble.com.