Marshalling and Unmarshalling

From JRapid

Jump to: navigation, search

Contents

Description

JRapid applications' data is sent from server-side to UI and back in XML format. Services and Controller are responsible for the processes of generating and distributing the data. The marshalling and unmarshalling processes are responsible for transforming objects to their XML representation and back.

Classes involved

The DefaultMarshaller class handles marshalling from server to client, and can be found in the xml package of your source code. For each entity, the default marshaller will contain a marshal method and a marshalFull method:

public void marshal(Writer writer, 
                    String nodeName, 
                    <Entity> obj, 
                    Boolean showSecondary) throws Exception {...

public void marshalFull(Writer writer, 
                        String nodeName, 
                        <Entity> obj, 
                        Boolean foo) throws Exception {...

For each subset, a new class to handle marshalling is created. The class is named <Entity><Subset>Marshaller. It will also create a marshal method similar to the above:

public void marshal(Writer writer, 
                    String nodeName, 
                    <Entity> obj, 
                    Boolean showSecondary) throws Exception {...

JRapid uses Castor XML mappings for unmarshalling. Castor is an open source data binding framework. The Castor XML mappings are found in the <Entity>Full.xml files

Example

In the MainControllerAbstract class there is an example of how DefaultMarshaller objects are instantiated. For each path providing a service that is created, an instance of DefaultMarshaller is created. The following example is the controller methods created for a Company entity:

  controller.registerXmlRpcService(CompanyServices.class, "removeMany", "Company.remove");
  controller.registerXmlService(GET, "/Company", CompanyServices.class, "findPage", new DefaultMarshaller(), true);
  controller.registerXmlService(GET, "/Company/([0-9,]+)" , CompanyServices.class, "find", new DefaultMarshaller(true), false);
  controller.registerXmlService(POST, "/Company/([0-9,]+)", CompanyServices.class, "store", new DefaultMarshaller(true, "com/libraryproject/xml/CompanyFull.xml"), false);			

In the above example, the controller needs to marshall the companies in "find" and "findPage" and unmarshalls the company when storing. The final POST method passes in the Castor XML file to the constructor of the DefaultMarshaller.

To see how the company values are structured when marshalled, you can create your own HTTP request and view the output. The layout for the Company find method directly above will be http://<jrapidServer>/<yourProject>/xml/<Entity>/<Parameter>. Typing http://demo.jrapid.com/libraryproject/xml/Company/1 for my project libraryproject on the demo server into my browser results in the following response:

  <company id='1'  style='' >
     <name>JRapid</name>
     <address>Cordoba 900</address>
     <expiration></expiration>
     <active>false</active>
  </company>		

See also

Personal tools