0
votes

I am new to Jersey framework. Just want to know what are the advantages of using exception mapper. If I want to throw any error to the rest consumer I can do the following in the method, Response.status(502).entity("Server error during registration").build(); Why it is recommended to use exceptionMapper, please help me understand?

1

1 Answers

0
votes

In the particular case of your example, it will have the same effect.

In case the exception is not thrown by you directly but in some code you are calling, for instance from a library, you would have to catch that exception to "map" it manually, in order to have the desired behavior.

Now if you want to treat different kind of exceptions in different ways, you will probably need to have several catch blocks, or use instanceof intensively. And for each new entrypoint of your service, you will have to repeat this exception handling business.

ExceptionMapper is just a clear/scalable way to intercept potential exceptions that may be thrown within the implementation of your service.