I am using Jackson 2 library and I am trying to read a JSON response, which looks like:
{ "value":"Hello" }
When value is empty, JSON response looks like:
{ "value":{} }
My model POJO class looks like this
public class Hello {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
The problem is that when response looks like {value:{}}, Jackson is trying to read an Object, but my model class field is a string, so it throws an Exception:
JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token.
My question is how Jackson can successfully read JSONs who look like:
{"value":"something"}
and at the same time if response looks like this {"value":{}} (empty response for me), pass null to value field of my Hello model class.
I am using the code below in order to read JSON string:
String myJsonAsString = "{...}";
ObjectMapper mapper = new ObjectMapper();
mapper.readValue(myJsonAsString , Hello.class);