views:

36

answers:

2

Greetings, I have a complicated scenario to handle. I have a wsdl file which uses a particular XML schema. The XML schema is actually a handcrafted implementation of a specification. There is also a Java based implementation of the same specification. So the XSD used in WSDL and Java classes at hand are quite similar, but not exactly same. Almost all web service stacks allow creating classes from WSDL or creating WSDL from Java class annotations. What I want to do, is to use the WSDL and bind XSD used in the wsdl to existing java classes. Should/can I do this by manually replacing generated Java classes with existing ones? Is it a matter of changing type names in config files and moving binding annotations to existing classes? If you know any best practices, or java web service stacks that support this kind if flexibility in a practical way, your response would be much appreciated.

Best Regards Seref

+2  A: 

I suggest Spring's Web Services module, which has no code generation involved, but provides a clean separation of concerns. Different concerns are broken out nicely by allowing you to provide your WSDL and existing schema(s) on one side (contract first), your existing Java-based domain model on the other, and a way to plugin in your OXM (Object-XML Mapping) technology of choice.

Since you have hand-crafted WSDL/schema and hand-crafted Java classes, the real work will be in configuring your OXM. I prefer JiBX as it keeps the concerns separated (no XML annotation garbage mixed into your domain) with JAXB as a backup if the learning curve looks too steep. Spring Web Services supports several other OXM frameworks, and you can even use several different ones at once.

As far as best-practices, I consider hand-crafted code a best practice, though I may be in the minority. If you generate classes from XML you end up with classes that are simple data containers with no behavior (assuming you want to regenerate them whenever your WSDL/XSD changes). This is bad if you favor the object-oriented paradigm because you end up having to place your "business logic" in utilities/helpers/services etc. instead of in the domain objects where it really belongs. This is one reason I favor JiBX. I can make very nice OO objects with behavior, a nice clean schema that doesn't necessarily match the objects, and can manage changes to either side with a mapping file similar to how hibernate does it for ORM (Object-Relational Mapping). You can do the same with JAXB, but that requires embedding XML structure into your object model, and binds a single XML representation to it (whereas with JiBX you can have many).

SingleShot
+1 - exactly right.
duffymo
Thanks, this is really helpful. Data container, auto generator classes easily become something you have to map to other classes. Most of web service related development time goes to finding ways of carrying data in these classes around. I'll check out JibX and Spring web services.
I've thought about this some more; do you think it would make sense to use this as a replacement for some object mapping libraries, like dozer? Dozer gave me lots of issues due to type erasure in generics, maybe an object1 ---> xml ----> object2 type of mechanism may replace that.
+1  A: 

MOXY (I'm the tech lead) was designed for instances where you have an existing XML Schema and an exsting object model. It accomplishes this through XPath based mapping and can ever handle cases where the models are not that similar:

MOXy also has an external binding file:

MOXy is a JAXB implementation with extensions (A couple of which are mentioned above). IF you go ahead with Spring, MOXy is configured as a JAXB implementation, and you need to add a jaxb.properties file in with your model classes with the following entry:

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
Blaise Doughan
Cool. This looks like a viable alternative to JiBX. I will check it out!
SingleShot
indeed. thanks Blaise, I'll certainly check this one.