Jackson serializes interface methods only

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 } 
+10
source share
4 answers

Just comment on your interface so that Jackson builds the data fields according to the class of the interface, and not the base class of objects.

 @JsonSerialize(as=Interf.class) public interface Interf { public boolean isNo(); public int getCountI(); public long getLonGuis(); } 
+12
source

You have two options:

1) put the @JsonSerialize annotation in your interface (see @ answer broc.seib )

2) or use a specific writer for serialization (as of Jackson 2.9.6):

 ObjectMapper mapper = new ObjectMapper(); String str = mapper.writerFor(Interf.class).writeValueAsString(interf); 
+1
source

The solution did not work for me. could you help me?

My interface:

 @JsonSerialize(as = ProjectionAgenda.class) //@JsonTypeInfo(use = NAME, include = WRAPPER_OBJECT) //@JsonSubTypes({@JsonSubTypes.Type(value = Agenda.class, name = "Object;")}) public interface ProjectionAgenda { Long getId(); String getResumo(); String getCompromisso(); @JsonFormat(pattern = "dd/MM/yyyy") Timestamp getInicio(); @JsonFormat(pattern = "HH:mm") Timestamp getHora(); @JsonFormat(pattern = "dd/MM/yyyy") Timestamp getFim(); String getLocal(); } 

Output new ObjectMapper().writeValueAsString(entity)

[[16678,"text in herer","text in here",1508436000000,1508446800000,"text in here",1508436000000] .... others. The Beacause entity is ArrayList ProjectionAgenda.

Kinds.

0
source

if the serialized class (in your case: Impl.class) comes from an external library, so I cannot add any annotations to it .

How can I do it?

0
source

Source: https://habr.com/ru/post/953299/


All Articles