Search code examples
javajacksonjax-rsjersey-2.0jackson2

Jersey 2.x ExceptionMapper Parent Mapping


EDIT: It was an issue in earlier versions of ackson-jaxrs-base ,which is resolved in jackson-jaxrs-base-2.8. https://github.com/FasterXML/jackson-jaxrs-providers/issues/22

I am struggling with this issue, with exception mapper. I want to map all child of com.fasterxml.jackson.core.JsonProcessingException in a sinlge ExceptionMapper.

here is my code:

@Provider
public class JsonProcessingExceptionMapper implements ExceptionMapper<JsonProcessingException> {

    @Override
    public Response toResponse(JsonProcessingExceptionexception) {
        return Response.status(Response.Status.BAD_REQUEST)
                .entity("json parsing error!")).build();
    }

works fine with following code:

@Provider
public class JsonMappingExceptionMapper implements ExceptionMapper<JsonMappingException> {

    @Override
    public Response toResponse(JsonMappingException exception) {
        return Response.status(Response.Status.BAD_REQUEST)
                .entity("parsing error!").build();
    }
}

EDIT: JsonProcessingException is parent of JsonMappingException what exactly i am doing wrong here ?


Solution

  • Answering my own question:

    I got my issue fixed in following way. updated jackson jars to 2.8x and added following annotation.

    @Priority(4000)
    @Provider
    public class JsonMappingExceptionMapper implements ExceptionMapper<JsonMappingException> {
    
        @Override
        public Response toResponse(JsonMappingException exception) {
            return Response.status(Response.Status.BAD_REQUEST)
                    .entity("parsing error!").build();
        }
    }
    

    it works as expected now.