Jersey unable to catch any Jackson Exception

前端 未结 2 1600
天命终不由人
天命终不由人 2020-12-17 20:03

For my REST api I\'m using jersey and ExceptionMapper to catch global exceptions. It works well all the exception my app throws but I\'m unable to catch exception thrown by

2条回答
  •  庸人自扰
    2020-12-17 20:27

    Had the same problem.
    The problem is that JsonMappingExceptionMapper kicks in before your mapper.

    The actual exception is of class com.fasterxml.jackson.databind.exc.InvalidFormatException and the mapper defines com.fasterxml.jackson.jaxrs.base.JsonMappingException, so it's more specific to the exception.
    You see, Jersey's exception handler looks to find the most accurate handler (see org.glassfish.jersey.internal.ExceptionMapperFactory#find(java.lang.Class, T)).

    To override this behavior, simply disable the mapper from being used:

    1. Using XML: jersey.config.server.disableAutoDiscovery true

    2. Using code: resourceConfig.property(CommonProperties.FEATURE_AUTO_DISCOVERY_DISABLE, true); where resourceConfig is of type org.glassfish.jersey.server.ServerConfig.


    You can also write your own specific mapper:

    public class MyJsonMappingExceptionMapper implements ExceptionMapper
    

    But I think it's an over kill.

提交回复
热议问题