0

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",
}
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
flavio.donze
  • 7,432
  • 9
  • 58
  • 91
  • If I understand your question, do you want to ignore a particular private field "noGetter" ? – Sambit Aug 08 '19 at 16:48
  • getNoGetter() is a public method which is not actually a getter of a field but only returns a "generated" String see getNoGetter() in the class. I want to only serialize private fields and ignore all other methods. – flavio.donze Aug 08 '19 at 20:01

0 Answers0