Tips and tricks for using JiBX
start > axis2-jibx > jibx2wsdl > example2

example2

Created by dsosnoski. Last edited by dsosnoski, 261 days ago. Viewed 2,318 times. #11
This example (included in the distribution} demonstrates using Jibx2Wsdl with a simple service class and arrays. To use it, follow the readme.txt instructions in the distribution.

The code itself is very similar to example1, except for using arrays rather than Java 5 generic lists. This example also includes a customization file, though. To use it, remove the comment markers from the two lines shown commented out in the build.xml snippet below (lines 88-89 in the file):

<java classpathref="jibx-classpath"
        classname="org.jibx.ws.wsdl.Jibx2Wsdl" fork="true" failonerror="true">
      <arg value="-p"/>
      <arg value="start/bin"/>
      <arg value="-s"/>
      <arg value="start/src"/>
      <arg value="-t"/>
      <arg value="gen"/>
      <arg value="--strip-prefixes=m_"/>
<!--      <arg value="-f"/>
      <arg value="custom.xml"/>   -->
      <arg value="com.sosnoski.ws.library.jibx2wsdl.BookServer2"/>
    </java>

Then run "ant" again, and this time you'll use the custom.xml customization file for Jibx2Wsdl, shown here:

<custom force-classes="true" strip-prefixes="m_">
  <package name="com.sosnoski.ws.library.jibx2wsdl"
      namespace="http://sosnoski.com/ws/library">
    <class name="Book" requireds="type title isbn authors">
      <field field="m_type" attribute="type"/>
      <field field="m_isbn" attribute="isbn"/>
    </class>
    <class name="Type" requireds="description name"/>
  </package>
</custom>

This customization file gives Jibx2Wsdl details of how you want your data classes to be represented in XML. The package element says to use a specific namespace for the classes in the com.sosnoski.ws.library.jibx2wsdl package. The first class element says that for the Book class in that package you want the "type", "title", "isbn", and "authors" values to all be required. The field elements nested within this class element say to use attributes for the XML representations, rather than elements as are the default for java.lang.String and other object types. The second class element says what the required values are in the Type class.

Here's the resulting schema definition for these data classes:

<xsd:schema … xmlns:tns="http://sosnoski.com/ws/library"
    elementFormDefault="qualified"
    targetNamespace="http://sosnoski.com/ws/library">
  <xsd:complexType name="book">
    <xsd:annotation>
      <xsd:documentation>Library representation of a book.</xsd:documentation>
    </xsd:annotation>
    <xsd:sequence>
      <xsd:element type="xsd:string" name="title"/>
      <xsd:element name="authors">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element type="xsd:string" name="author" minOccurs="0" maxOccurs="unbounded"/>
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
    </xsd:sequence>
    <xsd:attribute type="xsd:string" use="required" name="type"/>
    <xsd:attribute type="xsd:string" use="required" name="isbn"/>
  </xsd:complexType>
  <xsd:complexType name="type">
    <xsd:sequence>
      <xsd:element type="xsd:string" name="description"/>
      <xsd:element type="xsd:string" name="name"/>
    </xsd:sequence>
    <xsd:attribute type="xsd:int" use="required" name="count"/>
  </xsd:complexType>
</xsd:schema>

Note that the appropriate values have been changed to attributes, and the values specified as required no longer have "minOccurs='0'".

no comments
snipsnap.org | Copyright 2000-2002 Matthias L. Jugel and Stephan J. Schmidt