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

example3

Created by dsosnoski. Last edited by dsosnoski, 261 days ago. Viewed 1,868 times. #12
This example (included in the distribution} demonstrates using Jibx2Wsdl with a simple service class and polymorphic types, along with Java 5 generic lists and enumerations. To use it, follow the readme.txt instructions in the distribution.

Here's a quick look at the service code and data model classes (from the start directory):

public class LibraryServer
{
    ...

public synchronized Item getItem(String isbn) { return m_itemMap.get(isbn); }

public synchronized List<Item> getItemsByType(String type) { ArrayList<Item> matches = new ArrayList<Item>(); for (int i = 0; i < m_itemList.size(); i++) { Item item = m_itemList.get(i); if (type.equals(item.getType())) { matches.add(item); } } return matches; }

public List<Type> getTypes() { ArrayList<Type> types = new ArrayList<Type>(); for (int i = 0; i < m_types.length; i++) { types.add(m_types[i]); } return types; }

public synchronized void addItem(Item item) throws AddDuplicateException { Item prior = getItem(item.getId()); if (prior == null) { internalAdd(item); } else { throw new AddDuplicateException(prior); } } }

public abstract class Item { protected final String m_id; private String m_type; private String m_title;

public Item(String id, String type, String title) { m_id = id; m_title = title; m_type = type; }

public String getId() { return m_id; }

public String getType() { return m_type; }

public String getTitle() { return m_title; } }

public class Book extends Item { public enum Format { HARDCOVER, TRADE_PAPERBACK, POCKET_PAPERBACK }

private String[] m_authors; private Format m_format;

public Book(String type, String isbn, Format format, String title, String[] authors) { super(isbn, type, title); m_format = format; m_authors = authors; }

public String getIsbn() { return getId(); }

public String[] getAuthors() { return m_authors; }

public Format getFormat() { return m_format; } }

public class Dvd extends Item { private String m_director; private String[] m_stars;

public Dvd(String type, String barcode, String title, String director, String[] stars) { super(barcode, type, title); m_director = director; m_stars = stars; }

public String getBarcode() { return getId(); }

public String getDirector() { return m_director; }

public String[] getStars() { return m_stars; } }

public class Type { … }

Since the service class (Library) methods don't reference the subclasses of Item directly, Book and Dvd need to be specified as extra classes when running Jibx2Wsdl. With that done, Jibx2Wsdl recognizes them as possible substitutions for the Item type and generates the WSDL and schema appropriately, as shown below:

<wsdl:definitions xmlns:tns="http://sosnoski.com/ws/library/jibx2wsdl/LibraryServer"
    … targetNamespace="http://sosnoski.com/ws/library/jibx2wsdl/LibraryServer">
  <wsdl:types>

<xsd:schema … xmlns:tns="http://sosnoski.com/ws/library/jibx2wsdl/LibraryServer" xmlns:ns1="http://sosnoski.com/ws/library/jibx2wsdl" elementFormDefault="qualified" targetNamespace="http://sosnoski.com/ws/library/jibx2wsdl/LibraryServer"> <xsd:import namespace="http://sosnoski.com/ws/library/jibx2wsdl" schemaLocation="jibx2wsdl.xsd"/> <xsd:complexType name="typeList"> <xsd:sequence> <xsd:element type="ns1:type" name="type" minOccurs="0" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="itemList"> <xsd:sequence> <xsd:element ref="ns1:item" minOccurs="0" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> <xsd:element name="addDuplicate"> <xsd:complexType> <xsd:sequence> <xsd:element ref="ns1:item" minOccurs="0"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="getItem"> <xsd:complexType> <xsd:sequence> <xsd:element type="xsd:string" name="isbn" minOccurs="0"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="getItemResponse"> <xsd:complexType> <xsd:sequence> <xsd:element ref="ns1:item" minOccurs="0"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="getItemsByType"> <xsd:complexType> <xsd:sequence> <xsd:element type="xsd:string" name="type" minOccurs="0"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="getItemsByTypeResponse"> <xsd:complexType> <xsd:sequence> <xsd:element type="tns:itemList" name="itemList" minOccurs="0"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="getTypes"> <xsd:complexType> <xsd:sequence/> </xsd:complexType> </xsd:element> <xsd:element name="getTypesResponse"> <xsd:complexType> <xsd:sequence> <xsd:element type="tns:typeList" name="typeList" minOccurs="0"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="addItem"> <xsd:complexType> <xsd:sequence> <xsd:element ref="ns1:item" minOccurs="0"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="addItemResponse"> <xsd:complexType> <xsd:sequence/> </xsd:complexType> </xsd:element> </xsd:schema>

</wsdl:types> … </wsdl:definitions>

<xsd:schema … xmlns:tns="http://sosnoski.com/ws/library/jibx2wsdl" elementFormDefault="qualified" targetNamespace="http://sosnoski.com/ws/library/jibx2wsdl"> <xsd:complexType name="type"> <xsd:sequence> <xsd:element type="xsd:string" name="description" minOccurs="0"/> <xsd:element type="xsd:string" name="name" minOccurs="0"/> </xsd:sequence> <xsd:attribute type="xsd:int" use="required" name="count"/> </xsd:complexType> <xsd:element substitutionGroup="tns:item" name="book"> <xsd:annotation> <xsd:documentation>Information about a book.</xsd:documentation> </xsd:annotation> <xsd:complexType> <xsd:complexContent> <xsd:extension base="tns:item"> <xsd:sequence> <xsd:element name="authors" minOccurs="0"> <xsd:complexType> <xsd:sequence> <xsd:element type="xsd:string" name="author" minOccurs="0" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="format" minOccurs="0"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:enumeration value="HARDCOVER"/> <xsd:enumeration value="TRADE_PAPERBACK"/> <xsd:enumeration value="POCKET_PAPERBACK"/> </xsd:restriction> </xsd:simpleType> </xsd:element> </xsd:sequence> </xsd:extension> </xsd:complexContent> </xsd:complexType> </xsd:element> <xsd:element substitutionGroup="tns:item" name="dvd"> <xsd:complexType> <xsd:complexContent> <xsd:extension base="tns:item"> <xsd:sequence> <xsd:element type="xsd:string" name="director" minOccurs="0"/> <xsd:element name="stars" minOccurs="0"> <xsd:complexType> <xsd:sequence> <xsd:element type="xsd:string" name="star" minOccurs="0" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:extension> </xsd:complexContent> </xsd:complexType> </xsd:element> <xsd:element type="tns:item" name="item"/> <xsd:complexType name="item"> <xsd:sequence> <xsd:element type="xsd:string" name="type" minOccurs="0"/> <xsd:element type="xsd:string" name="title" minOccurs="0"/> <xsd:element type="xsd:string" name="id" minOccurs="0"/> </xsd:sequence> </xsd:complexType> </xsd:schema>

The schema uses a substitution group to show the polymorphism. It also includes an inline simpleType definition for the enumeration type (inline because this type is only referenced in one place - if there were multiple references it would be promoted to a global type).

The example files also include a capture of the resulting XML, modified to reference the schema definitions. This is the sample.xml file, shown below:

<getItemsByTypeResponse xmlns:ns2="http://sosnoski.com/ws/library/jibx2wsdl"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:ns1="http://sosnoski.com/ws/library/jibx2wsdl/LibraryServer"
    xmlns="http://sosnoski.com/ws/library/jibx2wsdl/LibraryServer"
    xsi:schemaLocation="http://sosnoski.com/ws/library/jibx2wsdl/LibraryServer gen/LibraryServer.xsd">
  <itemList>
    <ns2:book>
      <ns2:type>scifi</ns2:type>
      <ns2:title>Infinity Beach</ns2:title>
      <ns2:id>0061020052</ns2:id>
      <ns2:authors>
        <ns2:author>McDevitt, Jack</ns2:author>
      </ns2:authors>
      <ns2:format>POCKET_PAPERBACK</ns2:format>
    </ns2:book>
    <ns2:book>
      <ns2:type>scifi</ns2:type>
      <ns2:title>Aristoi</ns2:title>
      <ns2:id>0812514092</ns2:id>
      <ns2:authors>
        <ns2:author>Williams, Walter Jon</ns2:author>
      </ns2:authors>
      <ns2:format>POCKET_PAPERBACK</ns2:format>
    </ns2:book>
    <ns2:book>
      <ns2:type>scifi</ns2:type>
      <ns2:title>Roadmarks</ns2:title>
      <ns2:id>0345253884</ns2:id>
      <ns2:authors>
        <ns2:author>Zelazny, Roger</ns2:author>
      </ns2:authors>
      <ns2:format>POCKET_PAPERBACK</ns2:format>
    </ns2:book>
    <ns2:dvd>
      <ns2:type>scifi</ns2:type>
      <ns2:title>Imposter</ns2:title>
      <ns2:id>B0000640VR</ns2:id>
      <ns2:director>Fleder, Gary</ns2:director>
      <ns2:stars>
        <ns2:star>Sinise, Gary</ns2:star>
        <ns2:star>Stowe, Madeleine</ns2:star>
      </ns2:stars>
    </ns2:dvd>
    <ns2:dvd>
      <ns2:type>scifi</ns2:type>
      <ns2:title>The Thirteenth Floor</ns2:title>
      <ns2:id>0767821629</ns2:id>
      <ns2:director>Rusnak, Josef</ns2:director>
      <ns2:stars>
        <ns2:star>Bierko, Craig</ns2:star>
        <ns2:star>Mueller-Stahl, Armin</ns2:star>
      </ns2:stars>
    </ns2:dvd>
    <ns2:book>
      <ns2:type>scifi</ns2:type>
      <ns2:title>The Dragon Never Sleeps</ns2:title>
      <ns2:id>0445203498</ns2:id>
      <ns2:authors>
        <ns2:author>Cook, Glen</ns2:author>
      </ns2:authors>
      <ns2:format>POCKET_PAPERBACK</ns2:format>
    </ns2:book>
  </itemList>
</getItemsByTypeResponse>
no comments
snipsnap.org | Copyright 2000-2002 Matthias L. Jugel and Stephan J. Schmidt