ghulam-e-mustafa
ghulam-e-mustafa

Reputation: 1

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 ?

Upvotes: 0

Views: 710

Answers (2)

ghulam-e-mustafa
ghulam-e-mustafa

Reputation: 1

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.

Upvotes: 0

kosbr
kosbr

Reputation: 388

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.

Upvotes: 1

Related Questions