Jackson serialize only interface methods

前端 未结 2 2074
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-04 01:36

I have one object A with some methods ma, mb, mc and this object implements an interface B with only ma and mb

相关标签:
2条回答
  • 2021-01-04 02:20

    You have two options:

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

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

    ObjectMapper mapper = new ObjectMapper();
    String str = mapper.writerFor(Interf.class).writeValueAsString(interf);
    
    0 讨论(0)
  • 2021-01-04 02:21

    Just annotate your interface such that Jackson constructs data fields according to the interface's class and not the underlying object's class.

    @JsonSerialize(as=Interf.class)
    public interface Interf {
      public boolean isNo();
      public int getCountI();
      public long getLonGuis();
    }
    
    0 讨论(0)
提交回复
热议问题