Lalit Mehra
Lalit Mehra

Reputation: 1273

Jersey Custom Exception Mapper for Invalid Json String

I am working on an application for which I required to build the response in case an invalid JSON payload is sent in the request.

Currently the exception message received at the server end is Unexpected character ('"' (code 34)): was expecting comma to separate OBJECT entries at [Source: org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream@310db662; line: 13, column: 7]

This is how I'm trying to achieve it ...

import java.util.ArrayList;
import java.util.List;

import javax.validation.ConstraintDeclarationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

@Provider
public class JsonParseExceptionMapper implements ExceptionMapper<JsonProcessingException> {

    private static Logger logger = LogManager.getLogger(JsonParseExceptionMapper.class.getName());

    @Override
    public Response toResponse(final JsonProcessingException ex) {

        System.out.println("inside json parse exception mappper");

        logger.error(messages.toString());
        return Response.status(Response.Status.BAD_REQUEST).entity(messages).type(MediaType.APPLICATION_JSON).build();
    }
}

I also have a similar ExceptionMapper which catches the ConstraintViolationException which is working fine.

I also tried a few different exceptions as suggested over the internet, to name a few

JsonProcessingException
BadRequestException
JsonParseException
InvalidFormatException
ConstraintDeclarationException

But None of the above exceptions seem to work in this case. Does anyone have any idea as to which exception should be used in conjunction with the ExceptionMapper to catch such cases.
Also, I guess it is possible to have more than one ExceptionMapper implementations running parallel to each other. Please correct if I'm wrong.

Edit: This is what I mean by an invalid JSON Payload, look out for the missing comma before checksum

{
    "payload": {
        "name":"rachel",
        "employeeid":"23443"
    }
    "checksum":""
}

Jersey v2.22.1
Spring v4.2.4-RELEASE

For some reason I'm not able to comment on any of the posts !!! Even the edit window has issues, I can't see any icon !!

I tried using RuntimeException, Exception and IOException as well but the ExceptionMapper didn't work for them too. I guess it takes a specific time of exception like the ConstraintViolationException which is in use with another ExceptionMapper and that one is working fine.

some part of web.xml jersey.config.beanValidation.enableOutputValidationErrorEntity.server true jersey.config.server.disableMoxyJson true

Upvotes: 3

Views: 3233

Answers (1)

Lalit Mehra
Lalit Mehra

Reputation: 1273

Initially I had registered JacksonFeature.class for Json Parsing which uses its own ExceptionMapper for JsonParseException.

I changed it to JacksonJaxbJsonProvider.class.

I'm now able to use my custom JsonParseExceptionMapper.

Had to change certain dependencies !!

Upvotes: 5

Related Questions