Jersey unable to catch any Jackson Exception

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 jackson.

For example one of my endpoint accept an object that contains an enum. If the Json in the request has a value that is not in the enum jersey throw this exception back

Can not construct instance of my.package.MyEnum from String value 'HELLO': value not one of declared Enum instance names: [TEST, TEST2]
at [Source: org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream@5922e236; line: 3, column: 1] (through reference chain: java.util.HashSet[0]->....)

Even though I have created this mapper

@Provider
@Component
public class JacksonExceptionMapper implements ExceptionMapper<JsonMappingException> {
  @Override
  public Response toResponse(JsonMappingException e) {
    ....
  }
}

The code never reach this mapper.

Is there anything we need to do in order to catch these exceptions?

EDIT Note: I have jus tried being less general and instead of JsonMappingException I use InvalidFormatException in this case the mapper is called. But I still don't understand because InvalidFormatException extends JsonMappingException and should be called as well


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:

  • Using XML: <init-param> <param-name>jersey.config.server.disableAutoDiscovery</param-name> <param-value>true</param-value> </init-param>

  • 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<JsonMappingException>
    

    But I think it's an over kill.

    链接地址: http://www.djcxy.com/p/89522.html

    上一篇: Xcode中分段控件中文本的选定状态

    下一篇: 泽西无法抓住任何杰克逊例外