Search code examples
web-servicesrestapache-axisrestful-architecture

Parsing REST vs SOAP web services


I am working with a public RESTful API. The service works great but I couldn't find WADL anywhere. So, upon receiving the stream, I just use the standard DOM to parse it and convert it into my POJOs (org.w3c.dom).

A couple of years ago, however, I worked with a SOAP service and used Axis2, where I was able to generate associated client classes to marshall the services into Java using the utility that came with Axis2.

I wonder if there is a similar API for REST that I could do a similar thing with provided I had the WADL. The way I do it now is not too terrible and is fast, it just seems outdated.


Solution

  • First off, not all RESTful services publish a WADL description of themselves; there's no requirement for them to do so, and some people feel strongly that it is wrong to do, that providing a description of what you do is a violation of the HATEOAS principle. But that's really beside the point from your perspective.

    What you want is to get an XSD schema for the XML documents being passed around; while a full WADL might not be available, a schema might. Failing that, you can write one based on what messages are sent. (In order to do a good job of DOM parsing, you already need a good idea of what data is actually in there; the rest is just syntax.) If you're writing it yourself, use a GUI authoring tool; there are some decent free ones about (I've used the one in the Eclipse WTP in the past, but that's surely not the only one) and writing schema by hand in a straight text editor gets annoying fast.

    Once you have a schema, feed it through JAXB (specifically the xjc tool) which will generate the binding classes for you. You'll still need to do some work to make a whole client, but it will take the grind out of the message parsing since you'll be able to make use of Unmarshaller to do the grunt work.