I have one object A with some methods ma, mb, mc, and this object implements the interface B only with ma and mb.
When I serialize B, I expect only ma and mb as a json response, but I get also mc.
I would like to automate this behavior so that all classes that I serialize are serialized based on an interface, not an implementation.
How can I do it?
Example:
public interface Interf { public boolean isNo(); public int getCountI(); public long getLonGuis(); }
Implementation:
public class Impl implements Interf { private final String patata = "Patata"; private final Integer count = 231321; private final Boolean yes = true; private final boolean no = false; private final int countI = 23; private final long lonGuis = 4324523423423423432L; public String getPatata() { return patata; } public Integer getCount() { return count; } public Boolean getYes() { return yes; } public boolean isNo() { return no; } public int getCountI() { return countI; } public long getLonGuis() { return lonGuis; } }
Serialization:
ObjectMapper mapper = new ObjectMapper(); Interf interf = new Impl(); String str = mapper.writeValueAsString(interf); System.out.println(str);
Answer:
{ "patata": "Patata", "count": 231321, "yes": true, "no": false, "countI": 23, "lonGuis": 4324523423423423500 }
Expected Answer:
{ "no": false, "countI": 23, "lonGuis": 4324523423423423500 }
source share