Fetch attribute or field name which caused JacksonParseException using jackson libraries

2.4k Views Asked by At

I am facing issue using json request mapped with a Java POJO object in my REST application. In the request json if i pass an integer value instead of a boolean value then application throws below exception

org.codehaus.jackson.JsonParseException: Unexpected character ('e' (code 101)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
 at [Source: org.apache.catalina.connector.CoyoteInputStream@6ab41dbb; line: 2, column: 17]

When i created a provider class which implements ExceptionMapper-JsonParseException- then i was able to custom define the error message into a common error message but i need specific error messages.

Expected Response:

employee id cannot be a "string" or isSupervisor can be either be "true" or "false"

Following are the jars used in the project:

jackson-core-asl-1.9.2.jar
jackson-mapper-asl-1.9.2.jar
jackson-jaxrs-1.9.2.jar
jackson-xc-1.9.2.jar
jersey jars of version 1.2
other jars not related to this issue
Jdk5 environment

This Application is a REST application running in apache tomcat 5 and in web.xml file, JacksonJsonProvider from Jackson library is configured and used for converting json to Java object conversion. Everything works as expected if provided json input request is properly mapped to the POJO object. Above shown exception is thrown only when there is a data mismatch.

2

There are 2 best solutions below

0
On

What you get is not a compatibility issue with mismatching data, but a hard JSON decoding error. So your best bet here would be to have a look at underlying input to see why you are getting something claiming to be JSON, but that isn't (since it is not valid JSON content).

As to property name; that should be available using JsonParser.getCurrentName(). If you want that you will need to construct JsonParser manually (via ObjectMapper.getFactory().createParser(...), then give that to readValue() method of ObjectMapper or ObjectReader.

Finally, if this was a databinding problem (and not decoding), exception itself would have bit more information.

0
On

When spring can not bind json to request object, there will be JsonMappingException. For instance, your field is integer but you send very large number which is greater than integer. Another exception is JsonParseException, when you sen request with incorrect type. Your request object is string and you send integer for instance. Both two exception are cause of HttpMessageNotReadableException. So you have to handle this exception. And you have to check cause in order to understand cause of exception. You can handler exception with.

    @ExceptionHandler(HttpMessageNotReadableException.class)

Then you have to understand cause of exception with using instanceof.

ex.getCause() instanceof JsonMappingException

or

ex.getCause() instanceof JsonParseException

With your exception you can catch invalid data. For JsonMappingException

  JsonMappingException exception = (JsonMappingException) ex.getCause();
  String invalidField = exception.getPath().get(0).getFieldName();

For JsonParseException

 JsonParseException exception = (JsonParseException) ex.getCause();
 String field = exception.getProcessor().currentName();

Above ex.getCause() belongs to HttpMessageNotReadableException.