Jersey Custom Exception Mapper for Invalid Json String

◇◆丶佛笑我妖孽 提交于 2019-12-12 18:13:05

问题


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


回答1:


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 !!



来源:https://stackoverflow.com/questions/35669774/jersey-custom-exception-mapper-for-invalid-json-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!