代码之家  ›  专栏  ›  技术社区  ›  briarheart

忽略@JsonValue注释而不进行子类化

  •  1
  • briarheart  · 技术社区  · 6 年前

    假设在某个第三方库中有一个类层次结构。其中一个班级 @JsonValue @JsonValue公司

    0 回复  |  直到 6 年前
        1
  •  3
  •   ldz    6 年前

    @JsonValue 通过属性或getter

    假设我们有课 Pojo @JsonValue公司 prop

    class Pojo {
        @JsonValue
        private String prop;
        private String prop2;
        // getters and setters
    }
    

    然后我们定义一个摘要 MixIn 类,重写特定成员或方法:

    abstract class MixIn {
        @JsonValue(false) // either annotating members
        private String prop;
    
        @JsonValue(false) // or annotating methods
        abstract String getProp();
    }
    

    这可以添加到 ObjectMapper 如下所示:

    objectMapper.addMixIn(Pojo.class, MixIn.class);
    

    道具 和 prop2