Jersey/Jackson Exception problem with ExceptionMapper

前端 未结 5 1243
野趣味
野趣味 2021-02-04 05:07

I\'m using Jersey to provide a java REST service to the outside world. I offer some functions that take JSON and I use the Jackson framework in combination with jersey to conver

5条回答
  •  臣服心动
    2021-02-04 05:54

    The reason that your custom ExceptionMapper doesn't catch the exceptions is because the Jackson library provides its own exception mappers, which catches the exception before it would be catched by your genereal exception mapper.

    Some suggest that you should implement your own exception mappers for JsonParseExceptionMapper and JacksonMappingExceptionMapper, but that, however, will give inconsistent results. See this issue on their GitHub.

    To solve this problem, you must make sure that none of the built-in exception mappers are registered. If you're using the jackson-jaxrs-providers library for JSON: jackson-jaxrs-json-provider, make sure that you are only registering either JacksonJaxbJsonProvider.class or JacksonJsonProvider.class in your Application class:

    public class MyApplication extends ResourceConfig {
        public MyApplication() {
            register(JacksonJaxbJsonProvider.class)
        }
    }
    

    Be aware of the JacksonFeature.class, as it registers the built in ExceptionMappers. Also stay away from the jersey-media-json-jackson library, which automatically will add some built in exception mappers, without you having to do anything at all.

提交回复
热议问题