title: Jersey Custom Exception Mapper for Invalid Json String
body: <p>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.</p>
  
  <p>Currently the exception message received at the server end is <strong>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]</strong></p>
  
  <p>This is how I'm trying to achieve it ...</p>
  
  <pre><code>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&lt;JsonProcessingException&gt; {
  
      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();
      }
  }
  </code></pre>
  
  <p>I also have a similar ExceptionMapper which catches the ConstraintViolationException which is working fine.</p>
  
  <p>I also tried a few different exceptions as suggested over the internet, to name a few</p>
  
  <pre><code>JsonProcessingException
  BadRequestException
  JsonParseException
  InvalidFormatException
  ConstraintDeclarationException
  </code></pre>
  
  <p>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.<br>
  Also, I guess it is possible to have more than one ExceptionMapper implementations running parallel to each other. Please correct if I'm wrong.</p>
  
  <p><strong>Edit: This is what I mean by an invalid JSON Payload, look out for the missing comma before checksum</strong></p>
  
  <pre><code>{
      "payload": {
          "name":"rachel",
          "employeeid":"23443"
      }
      "checksum":""
  }
  </code></pre>
  
  <p>Jersey v2.22.1<br/>
  Spring v4.2.4-RELEASE</p>
  
  <p><strong>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 !!</strong></p>
  
  <p>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.</p>
  
  <p>some part of web.xml
  
              jersey.config.beanValidation.enableOutputValidationErrorEntity.server
              true
          
          
              jersey.config.server.disableMoxyJson
              true
          </p>