Jersey Exception mappers not working when jackson deserialization fails

后端 未结 5 2036
借酒劲吻你
借酒劲吻你 2020-12-29 06:57

I am using Jersey 2.10 with Jackson serialization/deserialization feature in my REST API.

My idea is to make my REST API to always return a standard JSON error resp

5条回答
  •  不知归路
    2020-12-29 07:40

    Starting with Jersey 2.26 (1, 2) it should be enough to annotate the custom exception mapper with a sufficiently high Priority (high here meaning a low, strictly positive number). To override the “default” mappers provided by org.glassfish.jersey.media:jersey-media-json-jackson (to register(JacksonFeature.class)) we only provide these two custom mappers:

    @Provider
    @Priority(1)
    public class JsonMappingExceptionMapper implements ExceptionMapper {
      /* ... */
    }
    
    @Provider
    @Priority(1)
    public class JsonParseExceptionMapper implements ExceptionMapper {
      /* ... */
    }
    

    Unfortunately JAX-RS 2 Spec disregards priorities and only states:

    When choosing an exception mapping provider to map an exception, an implementation MUST use the provider whose generic type is the nearest superclass of the exception.

    Not registering JacksonFeature.class and registering JacksonJaxbJsonProvider.class instead as mentioned in another answer did not lead to consistent results.

提交回复
热议问题