创建自定义杰克逊注释
2022-09-03 01:01:17
一个项目需要将杰克逊注释的以下组合组合在一起。那么,有没有办法创建另一个注释来避免丑陋的复制/粘贴:
public class A {
@JsonProperty("_id")
@JsonSerialize(using=IdSerializer.class)
@JsonDeserialize(using=IdDeserializer.class)
String id;
}
public class B {
@JsonProperty("_id")
@JsonSerialize(using=IdSerializer.class)
@JsonDeserialize(using=IdDeserializer.class)
String id;
}
public class C {
@CustomId // don't repeat that configuration endlessly
String id;
}
更新:我试过这个,但没有成功:-(
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside
@JsonProperty("_id")
@JsonSerialize(using=IdSerializer.class, include=JsonSerialize.Inclusion.NON_NULL)
@JsonDeserialize(using=IdDeserializer.class)
public @interface Id {}
public class D {
@Id
private String id;
}