I have 2 sets of XSD’s, one that generates RPC based calls, and another that defines the product specific methods. The RpcType object (generated by JAXB2) has a ‘setRpcOperation’ method defined by:
RpcType.setRpcOperation(JAXBElement<? extends RpcOperationType>)
That ‘RpcOperation’ object should be the ‘specific product method’ described above. One example is (also generated by JAXB2):
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "get-user-stats", propOrder = {
"reset"
})
public class GetUserStats {
protected Boolean reset;
/**
* Gets the value of the reset property.
*
* @return
* possible object is
* {@link Boolean }
*
*/
public Boolean isReset() {
return reset;
}
/**
* Sets the value of the reset property.
*
* @param value
* allowed object is
* {@link Boolean }
*
*/
public void setReset(Boolean value) {
this.reset = value;
}
}
Now, is it possible to create an instance of ‘GetUserStatus’ and add it to the RpcType object via setRpcOperation?
This type of scenario is common:
Below is one way this could be done:
Message Schema – message.xsd
Have one XML schema to represent your message envelope. Introduce one type to represent the body of the message. This type will be extended by the different payloads.
Payload Schema – customer.xsd
This schema corresponds to a specific type of message payload. Notice how the customer type extends the body type from the message schema.
org.example.message.package-info
This class was generated from message.xsd.
org.example.message.Message
This class was generated from message.xsd.
org.example.message.Body
This class was generated from message.xsd.
org.example.customer.package-info
This class was generated from customer.xsd.
org.example.customer.Customer
This class was generated from customer.xsd.
Demo Class
Output
EDIT #1
In response to your second question (Cast JAXB2 generated object to JAXBElement?)
I don’t see where the JAXBElement comes into play with this example. I am able to run the following code:
EDIT #2
After changing the import to import to http://www.iana.org/assignments/xml-registry/schema/netconf.xsd I’m seeing the same behaviour as you.
To set the property correctly you will need to do something like:
JAXBElement supplies the element name for the GetFailedLoginCount value. This is required because the element corresponding to the rpcOperation property is substitutable:
In your schema that imports netconf.xsd you should have an element of type “get-failed-login-count” that can be substituted for “rpcOperation”. This will be supplied as a QName to the JAXBElement. Since we used element name “foo” above the schema update would look like: