Codehaus XFire
DocumentationQuicklinksDevelopers
Sponsors |
Aegis is the default XFire binding which maps XML to POJOs. It supports code first development only at this point - i.e. you write your service in POJOs and it will generate the XML schema/wsdl for you. XML and Annotation Mapping OverviewAegis has a flexible mapping system so you can control how your beans are controlled. By default your POJOs are serialized based on their name and namespaces. If you have a class in the "org.codehaus.xfire" package named "Employee" it would be serialized in namespace "http://xfire.codehaus.org" with the local name "Employee" Fore example, the java class: public class Employee { private String name; private String title; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } } In XML this translates to: <Employee xmlns="http://xfire.codehaus.org"> <name>Santa Claus</name> <title>Chief Present Officer (CPO)</title> </Employee> In XML Schema this would become a complex type: <xsd:complexType name="Employee"> <xsd:sequence> <xsd:element name="name" type="xsd:string" minOccurs="0" maxOccurs="1/> <xsd:element name="title" type="xsd:string" minOccurs="0" maxOccurs="1/> </xsd:sequence> </xsd:complexType>
Supported Types
If you have constructors defined in your Java beans, make sure a default constructor (i.e. no arguments) is also declared. (Aegis needs a no-argument contstructor to instantiate client Java classes.) Controlling Mappings with XMLIts easy to control how your service and its beans are mapped to xml. If you are using Java 5.0 skip straight down to that section otherwise read on to learn how to configure serialization via mapping files. Mapping files must exist in the same package as your bean or service class on the class path. In the above example the mapping file would be named "/org/codehaus/xfire/Employee.aegis.xml", with the following format: <mappings> <mapping uri="" name=""> <method name="methodName"> <return-type mappedName="" componentType=""/> <parameter index="" mappedName=""/> </method> <property name="" mappedName="" style="attribute|element" componentType=""/> </mapping> </mappings> When you are creating ClassName.aegis.xml files to control type mapping, you will have only one mapping element, and you don't need any attributes on the mapping element. You use property elements to specify how to map your properties. You must supply name= to select the property. There are more examples of specific cases below. Note that <method> is used to configure methods on your service and <property> is used to configure properties on your javabeans. Controlling NamingLets pretend that in the above example you would like the elements names to be capatilized and in the namespace "urn:north-pole:operations". You could achieve this through a mapping file like so: <mappings xmlns:np="urn:north-pole:operations"> <mapping name="np:Employee"> <property name="name" mappedName="Name"/> <property name="title" mappedName="Title"/> </mapping> </mappings> Notice that the namespace was declared on the mappings element and then the prefix was used to specify the element QNames for the name/title properties. This will result in a mapping like so: <np:Employee xmlns:np="urn:north-pole:operations"> <np:Name>Santa Claus</np:Name> <np:Title>Chief Present Officer (CPO)</np:Title> </np:Employee> Ignoring propertiesIf you don't want to serialize a certain property it is easy to ignore it: <mappings> <mapping> <property name="propertyName" ignore="true"/> </mapping> </mappings> Controlling minOccurs and nillableThe default Aegis mapping is to assume that, since any Java object can be null, that the corresponding schema elements should have minOccurs of 0 and nillable of true. There are properties on the mappings for to control this. <mappings>
<mapping>
<property name='everpresentProperty' minOccurs='1' nillable='false'/>
</mapping>
<mappings>
Setting Default minOccurs and nillable Parameters from JavaIf you have many properties, and you want most, or all of them, to have a minOccurs other than 0 or a nillable other than false, you can change the defaults for Aegis from Java code (amongst other places). AnnotationServiceFactory serviceFactory = new AnnotationServiceFactory(); // We 'happen to know' what types are used. AegisBindingProvider binder = (AegisBindingProvider)serviceFactory.getBindingProvider(); DefaultTypeMappingRegistry tmr = (DefaultTypeMappingRegistry)binder.getTypeMappingRegistry(); // here we disuade XFire from its rather annoying tendency to assume that, just because // anything in Java can be null, that we want to advertise all that nullity all over. Configuration configuration = tmr.getConfiguration(); configuration.setDefaultMinOccurs(1); configuration.setDefaultNillable(false); |