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

example4

Created by dsosnoski. Last edited by dsosnoski, 261 days ago. Viewed 1,618 times. #11
This example (included in the distribution} demonstrates using Jibx2Wsdl with a simple service class and polymorphic types including interfaces and multiple layers of derivation. To use it, follow the readme.txt instructions in the distribution.

Besides the default build, this example also includes two different variations using customizations, as specified by the custom1.xml and custom2.xml files. Running "ant prepare1" from the console builds the the version using custom1.xml, while running "ant prepare2" builds the version using custom2.xml. After uploading the generated LibraryServer.aar in each case you can again use "ant run" to try it out.

Here's a quick look at the service code and data model classes (from the start directory). First there are the classes from the com.sosnoski.ws.library.jibx2wsdl package; these are almost identical to the example3 code, so I'll just show them here in outline form:

public class LibraryServer2
{
    ...

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

public synchronized List<Item> getItemsByType(String type) { ArrayList<Item> matches = new ArrayList<Item>(); … return matches; }

public List<Type> getTypes() { ArrayList<Type> types = new ArrayList<Type>(); … 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 interface Item { public String getId();

public String getType();

public String getTitle();

public void setId(String id);

public void setType(String type);

public void setTitle(String title); }

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

// adds isbn (same as id), authors, and format properties to Item … }

public class Dvd implements Item { // adds barcode (same as id), director, and stars[] properties to Item }

Then there's another set of classes in the com.sosnoski.ws.library.jibx2wsdl.hd package:

public interface IDvd extends Item
{
    public String getDirector();

public String[] getStars();

public void setDirector(String director);

public void setStars(String[] stars); }

public abstract class Dvd implements IDvd { private String m_barcode; private String m_type; private String m_title; private String m_director; private String[] m_stars;

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

public String getId() { return m_barcode; } ...

public static class FutureDvd extends Dvd { private String m_format;

public FutureDvd(String type, String barcode, String title, String format, String director, String[] stars) { super(type, barcode, title, director, stars); m_format = format; }

public String getFormat() { return m_format; }

public void setFormat(String format) { m_format = format; } } }

public class BluRayDvd extends Dvd implements Item { private int m_releaseYear;

public BluRayDvd(String type, String barcode, String title, int year, String director, String[] stars) { super(type, barcode, title, director, stars); m_releaseYear = year; }

public int getReleaseYear() { return m_releaseYear; }

public void setReleaseYear(int releaseYear) { m_releaseYear = releaseYear; } }

public class HdDvd extends Dvd { private String m_studio;

public HdDvd(String type, String barcode, String title, String studio, String director, String[] stars) { super(type, barcode, title, director, stars); m_studio = studio; }

public String getStudio() { return m_studio; }

public void setStudio(String studio) { m_studio = studio; } }

So taken all together, there's one base interface (com.sosnoski.ws.library.jibx2wsdl.Item) with a pair of direct implementations, then an extension interface (com.sosnoski.ws.library.jibx2wsdl.hd.IDvd) with another four implementations, including one abstract class that can't be used directly and an inner class that can be used directly. Messy, I know - but deliberately so, since the purpose of this example was to make sure Jibx2Wsdl worked with different combinations of interfaces and subclasses.

As with example3, the service class (LibraryServer2) methods don't reference the subclasses of Item directly, so Book and Dvd from the com.sosnoski.ws.library.jibx2wsdl package, and IDvd, HdDvd, BluRayDvd, and Dvd$FutureDvd from the com.sosnoski.ws.library.jibx2wsdl.hd package, all 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, defining a structure of substitution groups that matches the interface and class derivation.

I'm not going to show the resulting WSDL and schema here, since they're long and you can easily recreate them yourself. But here's a sample of the actual XML message exchange:

<getItemsByTypeResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns1="http://ws.sosnoski.com/library" xmlns="http://ws.sosnoski.com/library">
  <ns1:itemList>
    <ns1:book>
      <ns1:id>0061020052</ns1:id>
      <ns1:type>scifi</ns1:type>
      <ns1:title>Infinity Beach</ns1:title>
      <ns1:author>McDevitt, Jack</ns1:author>
      <ns1:format>POCKET_PAPERBACK</ns1:format>
    </ns1:book>
    <ns1:book>
      <ns1:id>0812514092</ns1:id>
      <ns1:type>scifi</ns1:type>
      <ns1:title>Aristoi</ns1:title>
      <ns1:author>Williams, Walter Jon</ns1:author>
      <ns1:format>POCKET_PAPERBACK</ns1:format>
    </ns1:book>
    …
    <ns1:dvd>
      <ns1:id>0767821629</ns1:id>
      <ns1:type>scifi</ns1:type>
      <ns1:title>The Thirteenth Floor</ns1:title>
      <ns1:director>Rusnak, Josef</ns1:director>
      <ns1:star>Bierko, Craig</ns1:star>
      <ns1:star>Mueller-Stahl, Armin</ns1:star>
    </ns1:dvd>
    <ns1:hdDvd>
      <ns1:id>025192784927</ns1:id>
      <ns1:type>scifi</ns1:type>
      <ns1:title>Serenity</ns1:title>
      <ns1:director>Wheden, Joss</ns1:director>
      <ns1:star>Baccarin, Morena</ns1:star>
      <ns1:star>Baldwin, Adam</ns1:star>
      <ns1:star>Fillion, Nathan</ns1:star>
      <ns1:star>Torres, Gina</ns1:star>
      <ns1:star>Tudyk, Alan</ns1:star>
      <ns1:studio>Universal</ns1:studio>
    </ns1:hdDvd>
    <ns1:bluRayDvd releaseYear="2006">
      <ns1:id>012236191551</ns1:id>
      <ns1:type>scifi</ns1:type>
      <ns1:title>Stargate</ns1:title>
      <ns1:director>Emmerich, Roland</ns1:director>
      <ns1:star>Ackerman, Robert</ns1:star>
      <ns1:star>Allen, Rae</ns1:star>
      <ns1:star>Avari, Erick</ns1:star>
      <ns1:star>Cruz, Alexis</ns1:star>
      <ns1:star>Danziger, Kenneth</ns1:star>
    </ns1:bluRayDvd>
    …
  </ns1:itemList>
</getItemsByTypeResponse>

This example also includes two different customization examples. The first one, custom1.xml, makes some of the values required and also specifies that some of the values are to be represented using either attributes or child elements, rather than relying on the default behavior of Jibx2Wsdl (which is to use attributes for primitives and child elements for objects). It also uses a force-mapping="true" attribute to say that all classes named in the customization are to be included in the binding, which eliminates the need to list all the classes on the Jibx2Wsdl command line. Here's that customization file, along with the corresponding sample message:

<custom force-classes="true" property-access="true" add-constructors="true">
  <package name="com.sosnoski.ws.library.jibx2wsdl" force-mapping="true">
    <class name="Item" requireds="@id @type title"/>
    <class name="Book" requireds="@format authors"/>
    <class name="Dvd" requireds="/director stars"/>
    <class name="Type" requireds="description @name @count"/>
    <package name="hd">
      <class name="IDvd" requireds="@director stars"/>
      <class name="HdDvd" requireds="@studio"/>
      <class name="BluRayDvd" requireds="/releaseYear"/>
      <class name="Dvd$FutureDvd"/>
    </package>
  </package>
</custom>

<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/LibraryServer2" xmlns="http://sosnoski.com/ws/library/jibx2wsdl/LibraryServer2"> <ns1:itemList> <ns2:book id="0061020052" type="scifi" format="POCKET_PAPERBACK"> <ns2:title>Infinity Beach</ns2:title> <ns2:author>McDevitt, Jack</ns2:author> </ns2:book> <ns2:book id="0812514092" type="scifi" format="POCKET_PAPERBACK"> <ns2:title>Aristoi</ns2:title> <ns2:author>Williams, Walter Jon</ns2:author> </ns2:book> … <ns2:dvd id="0767821629" type="scifi"> <ns2:title>The Thirteenth Floor</ns2:title> <ns2:director>Rusnak, Josef</ns2:director> <ns2:star>Bierko, Craig</ns2:star> <ns2:star>Mueller-Stahl, Armin</ns2:star> </ns2:dvd> <ns3:hdDvd xmlns:ns3="http://sosnoski.com/ws/library/jibx2wsdl/hd" id="025192784927" type="scifi" director="Wheden, Joss" studio="Universal"> <ns2:title>Serenity</ns2:title> <ns3:star>Baccarin, Morena</ns3:star> <ns3:star>Baldwin, Adam</ns3:star> <ns3:star>Fillion, Nathan</ns3:star> <ns3:star>Torres, Gina</ns3:star> <ns3:star>Tudyk, Alan</ns3:star> </ns3:hdDvd> <ns3:bluRayDvd xmlns:ns3="http://sosnoski.com/ws/library/jibx2wsdl/hd" id="012236191551" type="scifi" director="Emmerich, Roland"> <ns2:title>Stargate</ns2:title> <ns3:star>Ackerman, Robert</ns3:star> <ns3:star>Allen, Rae</ns3:star> <ns3:star>Avari, Erick</ns3:star> <ns3:star>Cruz, Alexis</ns3:star> <ns3:star>Danziger, Kenneth</ns3:star> <ns3:releaseYear>2006</ns3:releaseYear> </ns3:bluRayDvd> … </ns1:itemList> </getItemsByTypeResponse>

The second customization examples, custom2.xml, excludes some values from the XML representation, and uses wrapper elements around the collections. It also uses different settings for force-mapping="true" at different levels of the customizations (see customize). Here's that customization file, along with the corresponding sample message:

<custom force-classes="true" property-access="true"
    add-constructors="true" namespace="http://sosnoski.com/ws/library"
    namespace-style="fixed">
  <package name="com.sosnoski.ws.library.jibx2wsdl" force-mapping="true">
    <class name="Item" optionals="@id @type title"/>
    <class name="Book" excludes="format" wrap-collections="true"
        requireds="authors"/>
    <class name="Dvd" excludes="director stars"/>
    <package name="hd" wrap-collections="true" force-mapping="false">
      <class name="IDvd"/>
      <class name="HdDvd" requireds="@studio" force-mapping="true"/>
      <class name="BluRayDvd" optionals="/releaseYear" force-mapping="true"/>
      <class name="Dvd$FutureDvd" force-mapping="true"/>
    </package>
  </package>
</custom>

<getItemsByTypeResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns1="http://sosnoski.com/ws/library" xmlns="http://sosnoski.com/ws/library"> <ns1:itemList> <ns1:book id="0061020052" type="scifi"> <ns1:title>Infinity Beach</ns1:title> <ns1:authors> <ns1:author>McDevitt, Jack</ns1:author> </ns1:authors> <ns1:format>POCKET_PAPERBACK</ns1:format> </ns1:book> <ns1:book id="0812514092" type="scifi"> <ns1:title>Aristoi</ns1:title> <ns1:authors> <ns1:author>Williams, Walter Jon</ns1:author> </ns1:authors> <ns1:format>POCKET_PAPERBACK</ns1:format> </ns1:book> … </ns1:dvd> <ns1:dvd id="0767821629" type="scifi"> <ns1:title>The Thirteenth Floor</ns1:title> </ns1:dvd> <ns1:hdDvd id="025192784927" type="scifi" studio="Universal"> <ns1:title>Serenity</ns1:title> <ns1:director>Wheden, Joss</ns1:director> <ns1:stars> <ns1:star>Baccarin, Morena</ns1:star> <ns1:star>Baldwin, Adam</ns1:star> <ns1:star>Fillion, Nathan</ns1:star> <ns1:star>Torres, Gina</ns1:star> <ns1:star>Tudyk, Alan</ns1:star> </ns1:stars> </ns1:hdDvd> <ns1:bluRayDvd id="012236191551" type="scifi"> <ns1:title>Stargate</ns1:title> <ns1:director>Emmerich, Roland</ns1:director> <ns1:stars> <ns1:star>Ackerman, Robert</ns1:star> <ns1:star>Allen, Rae</ns1:star> <ns1:star>Avari, Erick</ns1:star> <ns1:star>Cruz, Alexis</ns1:star> <ns1:star>Danziger, Kenneth</ns1:star> </ns1:stars> <ns1:releaseYear>2006</ns1:releaseYear> </ns1:bluRayDvd> … </ns1:itemList> </getItemsByTypeResponse>

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