I have a client (JVM1) talking to server (JVM2) in a RESTful way. I’m using JSON for payload mechanism.
When the server throws an Exception, I’m able to serialize it to JSON format and ship it over network. On the client side, I receive the response and see the content in JSON format.
The problem is that I’m not able to identify the specific type of Exception which is being thrown.
Given a serialized version of some exception in JSON format (it could be NullPointerException, IllegalStateException, even a checked-exception) how do I construct the object of the sametype back at client side.
I can add any wrapper classes and share it between client and server if needed.
Thanks in advance.
in general, it is a bad idea to deserialize arbitrary exceptions on the client side. for instance, the server could throw a subclass of SQLException which happens to be specific to the jdkc driver impl in use. the client side most likely does not have the jdbc driver in its classpath, so any attempt to deserialize the exception will result in some sort of crazy class loading exception. the best thing to do is convert the exception into a common class like “ServerException” with the relevant details as string members of this class (e.g. original exception name, message, stack trace, etc.).