com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No value type configured for ObjectReader

Rence Abishek :

I am receiving com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No value type configured for ObjectReader problem while executing below code

Jackson version is jackson-databind-2.9.9.jar

public <T> T parsingData(String body) {
  try {
    return getObjectMapper().reader().readValue(body);
  } catch (IOException ioe) {
     ioe.printStackTrace();
  }
}

Below exact Exception i'm getting in printStackTrace and can find the String body which is exposed in the exception.

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No value type configured for ObjectReader
at [Source: (String)"{
     "timestamp": "2019-06-04T09:36:50.086+02:00",
     "path": "/api/check/85358/checking/246syb-f3f2-4756-91da-dae3e8ce774b/test/22462da-c4e2-45ca-bd27-246/rows/8bc3965a-ae22-4d7f-b770-262sgs24/port",
     "status": 400,
     "error": "Internal Server Error",
     "message": "[owner.firstName2:size:1:30, owner.lastName2:size:1:30]",
     "errorCode": 200000
    }
    "; line: 1, column: 1]

Updated: Could you please explain me why am i receiving this exception? Is this because that I haven't provide Class or TypeReference?

Michał Ziober :

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No value type configured for ObjectReader

Means, Jackson does not know which type you would like to deserialise to. It does not work because of Type Erasure in Java.

It will work when you bind a class:

public static <T> T parsingData(String body) throws IOException {
    return new ObjectMapper().readerFor(A.class).readValue(body);
}

Or with Class param:

public static <T> T parsingData(String body, Class<T> clazz) throws IOException {
    return new ObjectMapper().readerFor(clazz).readValue(body);
}

See similar question for readValue method and how it could be solved: How do I parametrize response parsing in Java?

To understand readValue mehtod we need to take a look on documentation:

Method that binds content read from given JSON string, using configuration of this reader. Value return is either newly constructed, or root value that was specified with withValueToUpdate(Object).

but in your code you used pure reader without any configuration.

If you do not know a type just deserialise JSON to JsonNode which is probably the most generic way:

JsonNode root = new ObjectMapper().readTree(json);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=327201&siteId=1