Nozbe


Twitter
  
 
Archive
Minimize
 
July 31, 2010
Add to Technorati Favorites

Tomcat and Castor for returning XML from java servlets
Location: BlogsDave Amphlett's Personal BlogSoftware Development    
Posted by: Dave Amphlett Wednesday, June 04, 2008
I've been working on exposing one of my client's existing Java and C# API's via various 'web services'. The combination of Tomcat and Castor has a niggly issue that google didn't provide an instant answer for...
I hit a knarly problem in using Castor to return the XML dump of an object. I just keep getting empty results, and on inspecting the castor debugging log stuff in detail I can't get much sense out of it. googling and even stepping through the castor code which is quite laborious to do still didn't yield any useful insights in a reasonable amount of time. Finally after some early morning 'deep' googling and doggedly trying many many many combinations I finally established the fundamental problem:

By default, within Tomcat, Castor can't "see" the classes deployed under the WEB-INF directory. Not even the class making the call to castor! A class def not found exception would have helped diagnose this, but it was being caught and rethrown as something different :|

So here's an order of calling the Marshaller that I've found that DOES work...

import org.exolab.castor.xml.Marshaller;


                Marshaller marshaller = new Marshaller(pw);
                marshaller.setValidation(false);
                marshaller.setSuppressNamespaces(true);
                marshaller.setRootElement("results");
                marshaller.getResolver().setClassLoader(Thread.currentThread().getContextClassLoader());
                marshaller.marshal(oArray);


The setClassLoader line is the magic. Now if only I could get the mapping functionality to work I'd be really happy - I'll keep working on that for now!

Permalink |  Trackback

Your name:
Title:
Comment:
Add Comment   Cancel