Throw Jersey RESTful Web Services Error Messages

I have a RESTful web service that does some I / O and database operations to return the result.

There are several Exceptions that I would like to see, i.e. an exception because the expected variable is not set, or my database no longer exists, etc.

However, when I throw these exceptions, they are caught and terminated as an HTTP 500 (Internal Server Error) exception, and this is what the client receives (as opposed to the root exception).

I would like to see my exception here, instead of looking for application logs.

+4
source share
1 answer

You can implement ExceptionMapper, which returns everything you want. So, for example, it can put all exception from stacktrace in the answer.

Is this what you want on the client side? I'm not sure.
It can be quite a difficult task for a client to rebuild exceptions and modify it.

In addition, it is rather unsafe, since stacktrace will expose the internal server code to the client.

I suggest you declare some server error codes that make sense to the client and that you want to expose. Example:

  • 1000 - database connection failure.
  • 2000 - something else

So, for specific exceptions in ExceptionMapper, you can map server exceptions to error codes and then reassign them on the client side.

+2
source

All Articles