I have a class that extends RepresentationModel
from Spring Hateoas. I have no problem converting an object of this class to JsonNode
but exceptions are thrown when converting back from JsonNode
to this class using Jackson convertValue
.
And the reason for that is there is a links
field added to the object by RepresentationModel
and the field is not defined in the class–it’s inherited from RepresentationModel
. So upon calling the method convertValue(jsonNode, ObjExtendsRepresentationModel.class)
, I’m getting com.fasterxml.jackson.databind.exc.MismatchedInputException: no String-argument constructor/factory method to deserialize from String value ('self')
So my question is it possible to make Jackson go quiet and complete the conversion when convertValue
method comes across a json property that is not defined in the object class? So far, I’ve tried setting DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES
to false but this did not help at all.
Update:
public class AResource extends RepresentationModel<AResource> {
private final String aField;
private final String bField;
public AResource(String aField, String bField) {
this.aField = aField;
this.bField = bField;
}
}
...
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new ParameterNamesModule(JsonCreator.Mode.PROPERTIES));
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
JsonNode existing = mapper.convertValue(aResource, JsonNode.class);
...
AResource reverted = mapper.convertValue(existing, AResource.class);