5

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

When Jackson throws an exception, it automatically returns a string with the error description.

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

The problem is, it only catches the exception when I specify exactly the type of the Exception being thrown.

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

public class MyExceptionMapper implements ExceptionMapper<UnrecognizedPropertyException>

But if I change UnrecognizedPropertyException to PropertyBindingException (which the first extends), it won't work.

In short:

How can I create a generic exception mapper that catches any exception thrown by Jackson (or any other component of my app, for that matter)?

vixprogram
  • 61
  • 2

1 Answers1

0

try with

public class MyExceptionMapper implements ExceptionMapper<Exception> 

This should be the fallback for all Exceptions.

Jackson is looking for the the hierarchie from the exception up if it finds a suitable ExceptionMapper. It looks as long as there is something in the type hierarchy. So UnrecognizedPropertyException will be handled by PropertyBinding-Exception mapper but not the other way round because UnrecognizedPropertyException Mapper is more specific there could be many subclasses and then there cannot be determined which Mapper to take. So it only works upwards.

An because Exception is the base Exception everything ends up there.