Catching Any Exception Jackson Throws With One ExceptionMapper

I have a JAX-RS project that uses Jackson to handle JSON conversions.

When Jackson throws an exception, he automatically returns a string describing the error.

Since I want to return a custom JSON object, I created an ExceptionMapper .

The problem is that it only detects an exception when I specify exactly what type of exception is thrown.

For example, when the JSON sent to the method contains an unknown property, this works:

public class MyExceptionMapper implements ExceptionMapper<UnrecognizedPropertyException>

But if I changed UnrecognizedPropertyExceptionto PropertyBindingException(which the first extends), it will not work.

In short:

How do I create a generic exception mapping engine that catches any exception thrown by Jackson (or any other component of my application, for that matter)?

+4
source share
1 answer

try

public class MyExceptionMapper implements ExceptionMapper<Exception> 

This should be a backup for all Exceptions.

, ExceptionMapper. , -. , UnrecognizedPropertyException PropertyBinding-Exception mapper, , UnrecognizedPropertyException Mapper , , , Mapper . , .

, .

0

Source: https://habr.com/ru/post/1676511/


All Articles