Using the DTO Design Pattern with EJBs to Produce XML


In reference to my earlier experiments with SOAP and EJBs, Randy Gordon pointed out an article to me on using the Data Transfer Object design pattern to return XML from your EJBs.  This isn't (directly) a way of exposing a SOAP interface, but it does offer a fairly convenient method for returning XML data from an EJB method call.   

For those of you not familiar with DTOs, the pattern creates a single object (usually a regular Java Bean) as a container or aggregator for data being returned from a remote method call.  This avoids lot of fine grained network traffic from individual getter and setter calls to the bean.  DTOs have the usual getters and setters you'd expect in a bean, but can have other methods as well.  This technique makes use of the the JOX (Java Objects in XML) library to create a toXML() method for the bean.  The EJB session bean creates the DTO and populates it with the required data and then returns it as the result of the remote method call.  The calling object can then get XML by calling toXML() on the DTO.