I'm migrating an existing communication layer to use REST with JSON. The previous framework serialized all private fields and ignored public getters, I would like to accomplish the same with Jackson.
I would like to make as little changes on the model classes being transferred, so annotations are not an option.
I already found a solutions to serialize the private properties:
ObjectMapper mapper = ...
mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
But having this example POJO, the JSON string also contains the value of getNoGetter()
which I don't want:
"noGetter" : "another string"
public class PrivateObject {
private int id;
private String name;
public PrivateObject() {
}
public PrivateObject(int id, String name) {
this.id = id;
this.name = name;
}
public String toString() {
return id+": "+name;
}
public String getNoGetter() {
return "another string";
}
}
Is there a way to configure Jackson to just consider the private fields?
Expected result of new PrivateObject(1234, "test")
:
{
"id" : 1234,
"name" : "test",
}