0

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 ?

2 Answers2

1

Class com.fasterxml.jackson.jaxrs.base.JsonMappingExceptionMapper exists in the lib and it implements ExceptionMapper.

So in the first case this mapper is the more suitable than yours because it handles JsonMappingException, but yours handles common JsonProcessingException.

In the second case your mapper has the same level with this mapper, so it can be applied.

kosbr
  • 388
  • 2
  • 11
  • so, there is no way to map all child of JsonProcessingException in a single ExceptionMapper ? – ghulam-e-mustafa Aug 26 '16 at 04:52
  • I don't know if it possible to remove provider. Maybe you have to do workaround. But probably this http://stackoverflow.com/questions/17794372/removing-an-added-provider-in-jersey will help you. – kosbr Aug 26 '16 at 13:22
0

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.