How to log exception of Unable to process JSON in dropwizard

3,319 views
Skip to first unread message

Rohit Verma

unread,
Mar 30, 2017, 10:27:07 AM3/30/17
to dropwizard-user
Hi All,

Is there a way to find out which field of my pojo object caused Unable to process JSON error, or is there a way to log jackson exception which cause this error 

Steve Kradel

unread,
Mar 30, 2017, 3:36:26 PM3/30/17
to dropwizard-user
Yes: suppress the default exception mapper, and provide one like this (in this case we tell the client what it did wrong, but you could log instead or in addition to):

import java.util.LinkedHashMap;

 

import java.util.Map;


 

import javax.ws.rs.core.MediaType;

import javax.ws.rs.core.Response;

import javax.ws.rs.core.Response.Status;

import javax.ws.rs.ext.ExceptionMapper;

import javax.ws.rs.ext.Provider;


 

import com.fasterxml.jackson.core.JsonProcessingException;


 

@Provider

public class JsonInformativeExceptionMapper implements ExceptionMapper<JsonProcessingException> {


 

 
@Override

 
public Response toResponse(JsonProcessingException exception) {

 

 
Map<String,Object> entity = new LinkedHashMap<>();

 entity
.put("code", 400);

 

 
{

 
String message = exception.getOriginalMessage();

 
if (message != null) {

 
int pos = message.indexOf('\n');

 
if (pos == -1) {

 pos
= message.indexOf(" at [Source");

 
}

 
if (pos > 0) {

 message
= message.substring(0, pos);

 
}

 entity
.put("message", message);

 
}

 
}

 

 
if (exception.getLocation() != null) {

 entity
.put("location", String.format("line %d, column %d",  

 exception
.getLocation().getLineNr(),  

 exception
.getLocation().getColumnNr()));

 
}

 

 
return Response.status(Status.BAD_REQUEST)

 
.type(MediaType.APPLICATION_JSON_TYPE)

 
.entity(entity)

 
.build();

 
}

}

Chris Stromberger

unread,
Mar 31, 2017, 10:25:55 AM3/31/17
to dropwizard-user
I added this in the Application class to override the existing JsonProcessingExceptionMapper, did the trick for my application. The true parameter is for "showDetails".

jersey.register(new JsonProcessingExceptionMapper(true));
Reply all
Reply to author
Forward
0 new messages