Java Jackson mixin:如何将类中的字段忽略或重命名为树

Java Jackson mixin:如何将类中的字段忽略或重命名为树,java,jackson,mixins,objectmapper,Java,Jackson,Mixins,Objectmapper,我正试图从表示为树的类中进行深层过滤(重命名/忽略字段) 使用Jackson Mixin,我可以从根级别重命名或忽略字段。我想要实现的是如何在多个级别上进行过滤(重命名/忽略) 例如,我有一棵树,它有两个类。类A是根,B是树中深度的第二级。通过应用Jackson Mxiin,我想从根A过滤属性a2,从B过滤属性b1 表示根类的类A public class A { private String a1; private String a2; private B b; public

我正试图从表示为树的类中进行深层过滤(重命名/忽略字段)

使用Jackson Mixin,我可以从根级别重命名或忽略字段。我想要实现的是如何在多个级别上进行过滤(重命名/忽略)

例如,我有一棵树,它有两个类。类A是根,B是树中深度的第二级。通过应用Jackson Mxiin,我想从根A过滤属性a2,从B过滤属性b1

表示根类的类A

public class A {
  private String a1;
  private String a2;
  private B b;

  public A(String a1, String a2, B b) {
      this.a1 = a1;
      this.a2 = a2;
      this.b = b;
  }

  public String getA1() {
      return a1;
  }

  public void setA1(String a1) {
      this.a1 = a1;
  }

  public String getA2() {
      return a2;
  }

  public void setA2(String a2) {
      this.a2 = a2;
  }

  public B getB() {
      return b;
  }

  public void setB(B b) {
      this.b = b;
  }

}
public interface AMixIn {
  // Filter for A (implemented to filter second depth as well)
  @JsonIgnore
  String getA2();

  @JsonIgnore
  public BMixIn getB();
}

public interface BMixIn {
  // Filter for B
  @JsonIgnore
  public String getB1();

}
B级-二级深度

public class B {

      private String b1;
      private String b2;

      public B(String b2, String b1) {
          this.b1 = b1;
          this.b2 = b2;
      }

      public String getB1() {
          return b1;
      }

      public void setB1(String b1) {
          this.b1 = b1;
      }

      public String getB2() {
          return b2;
      }

      public void setB2(String b2) {
          this.b2 = b2;
      }
    }
过滤器

public class A {
  private String a1;
  private String a2;
  private B b;

  public A(String a1, String a2, B b) {
      this.a1 = a1;
      this.a2 = a2;
      this.b = b;
  }

  public String getA1() {
      return a1;
  }

  public void setA1(String a1) {
      this.a1 = a1;
  }

  public String getA2() {
      return a2;
  }

  public void setA2(String a2) {
      this.a2 = a2;
  }

  public B getB() {
      return b;
  }

  public void setB(B b) {
      this.b = b;
  }

}
public interface AMixIn {
  // Filter for A (implemented to filter second depth as well)
  @JsonIgnore
  String getA2();

  @JsonIgnore
  public BMixIn getB();
}

public interface BMixIn {
  // Filter for B
  @JsonIgnore
  public String getB1();

}
测试

public class SecondLevelTest {
  // Test
  private ObjectMapper mapper = null;
  private ObjectWriter writer = null;

  @Before
  public void setUp() {
      // init
      mapper = new ObjectMapper();
      mapper.setMixIns(ImmutableMap.<Class<?>, Class<?>>of(A.class, AMixIn.class));
      writer = mapper.writer().with(SerializationFeature.INDENT_OUTPUT);
  }

  @Test
  public void rename_field_jackson() throws JsonProcessingException {

      B b = new B("vb1", "vb2");
      A a = new A("va1", "va2", b);

      // I want to get this result
      // {
      // "a1" : "va1",
      // "b2" : "vb2"
      // }
      String json = writer.writeValueAsString(a);
      System.out.println(json);
  }
}
公共类二级测试{
//试验
私有对象映射器映射器=null;
私有ObjectWriter=null;
@以前
公共作废设置(){
//初始化
映射器=新的ObjectMapper();
setMixIns(ImmutableMap.>of(A.class,AMixIn.class));
writer=mapper.writer(),带有(SerializationFeature.INDENT_输出);
}
@试验
public void rename_field_jackson()引发JsonProcessingException{
B=新的B(“vb1”、“vb2”);
A=新的A(“va1”、“va2”、b);
//我想得到这个结果
// {
//“a1”:“va1”,
//“b2”:“vb2”
// }
字符串json=writer.writeValueAsString(a);
System.out.println(json);
}
}

这将为您提供所需的输出。将
setUp()
更改为同时使用
BMixIn

@Before
public void setUp() {
    // init
    mapper = new ObjectMapper();
    mapper.setMixIns(ImmutableMap.of(A.class, AMixIn.class, B.class, BMixIn.class));
    writer = mapper.writer().with(SerializationFeature.INDENT_OUTPUT);
}
并将
AMixIn
更改为展开
B

public interface AMixIn {
    // Filter for A (implemented to filter second depth as well)
    @JsonIgnore
    String getA2();

    @JsonUnwrapped
    public BMixIn getB();
}

忘记注册BMixIn导致其
@JsonIgnore
永远无法使用
@JsonUnwrapped
un nests
b
这样你就得到了一个平面结构。

我想你在找。它应该在
getB()
上,而不是忽略。我不明白,你能解释一下吗?也许你可以分享如何实现它!