This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
I am creating a REST style webservice, and I am using jibx xml binding framework to create the response. I configured jibx to use StAX as its parser. I have run into an issue however, and would like to know how other people solve it:
When there is an exception thrown during the binding process, the client could get back an incomplete xml response, since its being streamed. Something like the following:
What I would really like to do is just have an error response returned, ie catch the exception and modify the response. Currently the way I am solving this issue is by having jibx write to a StringWriter, then I spit that back, and if there is an exception I can clear it out and add my error response:
Is there a better way? The above solution fixes the problem, but now I am creating the entire response in memory. How do other people handle this problem? I'd rather not send back invalid xml...
It seems like a choice between being memory intensive for completeness, or being incomplete for better performance
Also keep in mind that this is not an exception in the business logic, and it should never happen. [ July 09, 2007: Message edited by: joe black ]
Which has been modified to show my point. It uses stax, and when an error is thrown while writing to the stream, I can recover gracefully and send back well formed xml with an error element.
Notice I am throwing an Exception. This would be similar to a JibxException being thrown during marshalling. However, I catch this exception, add an error element as the end element, then call writeEndDocument which closes all open structures. Can something similar be done with jibx? instead of sending back xml that is not well formed? How does any xml to object binding framework handle this use case?
Peter [ July 09, 2007: Message edited by: joe black ]