For a project, I'm forced to use certain webservices. I created a small java project that does nothing but open the correct httpconnection, writes input and gets the output. This output is generally xml text or json text. I'm now wondering on how to convert this text to java objects.
I can either do it manually by using the following OOP scheme: When calling a webservice, if it has output, a "handler" class must be supplied. this handler class must conform to this interface:
public interface Handler<T>
{
public T handle(Byte[] webserviceOutput)
}
And a helper class for specific XML cases:
public abstract class XMLHandler<T> implements handler<T>
{
public T handle(Byte[] webserviceOutput)
{
Document xmldoc = convertByteArrayToDocument();
return parseXmlDoc(xmlDoc);
}
public abstract T parseXmlDoc(Document xmlDoc);
}
I like how clean this interface is, but the actual implementations are very unclean and all involved parsing the entire input, well, line by line and calling alot of setters manually (in other words: no reflection is used).
On the other hand, libraries exist that convert json and xml into existing java objects. Their major downside is they are very unflexible because the names of fields must match exactly to those in the xml. This is a burden when dealing with crazy dba's who abbreviate all their fields into gibberish (for example field 'apb' which should be named 'adjustablePersonBudget'). another problem is the autio-conversions tend to be verbose. For example, the xml
<books>
<book></book>
<book></book>
...
</books>
results in java in:
rootObject.getBooks().getBookList()
instead of just rootObject.getBookList()
The obvious advantage of using libraries is you don't converter classes and just reflections.
What are you experiences and thoughts on these two approaches for enterprise software?