所以我有一个POJO类,它将json文档(我收到的)映射到java对象。
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonAutoDetect(fieldVisibility = ANY, getterVisibility = NONE, setterVisibility = NONE)
public class Product implements Serializable, UniqueKeyAware {
private static final long serialVersionUID = -7311148654827944888L;
@JsonIgnore
private String uniqueKey;
@JsonProperty("uniqueKey")
private String uniqueKeyV2;
@JsonProperty("gtin")
private String gtin;
@JsonProperty("printedGtin")
private String printedGtin;
@JsonProperty("tpnb")
private String tpnb;
@JsonProperty("tpnc")
private String tpnc;
@JsonProperty("tpna")
private String tpna;
@JsonProperty("itemNumber")
private String itemNumber;
@JsonProperty("catId")
private String catId;
@JsonProperty("styleCode")
private String styleCode;
@JsonProperty("description")
private String description;
@JsonProperty("brand")
private String brand;
@JsonProperty("isOwnLabel")
private Boolean isOwnLabel;
@JsonProperty("regulatedProductName")
private String regulatedProductName;
@JsonProperty("country")
private List<Country> region;
... // remove for simplicity
我想要的是从json创建4个单独文档的最佳方法。
所以,让我们定义4个类,它们是这个pojo类的子集。
出于可配置性目的,我需要自定义注释,如@ public,@ private,@ partner,@ priviledge,我将在每个字段上面写。在运行时,如果我指定例如在公共类中,将仅为那些写有@public注释的字段创建实例。
我需要实现这一点。我认为这可以通过在jackson库中创建一些钩子来实现。我需要在一天内完成,任何人都可以指导如何做到这一点。
最终产品应该是这样的:
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonAutoDetect(fieldVisibility = ANY, getterVisibility = NONE, setterVisibility = NONE)
public class Product implements Serializable, UniqueKeyAware {
private static final long serialVersionUID = -7311148654827944888L;
@JsonIgnore
private String uniqueKey;
@JsonProperty("uniqueKey") @public @private
private String uniqueKeyV2;
@JsonProperty("gtin") @public
private String gtin;
@JsonProperty("printedGtin") @public
private String printedGtin;
@JsonProperty("tpnb")@private
private String tpnb;
@JsonProperty("tpnc")@private
private String tpnc;
@JsonProperty("tpna")@priviledge
private String tpna;
... // removed for simplicity
以及以上例如@public实例将使用uniqueKey,gtin,printedGtin作为唯一属性。
答案 0 :(得分:1)
我找到了解决方案。我们需要的是杰克逊已经存在的@JsonView注释。我们首先需要创建这样的视图类:
public class Views
{
public static class Public{};
public static class Partner extends Public{};
public static class Priviledge extends Partner {};
public static class Private extends Priviledge {};
}
然后,我们的主要产品类将是这样的。
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonAutoDetect(fieldVisibility = ANY, getterVisibility = NONE, setterVisibility = NONE)
public class MyProduct implements Serializable, UniqueKeyAware {
private static final long serialVersionUID = -7311148654827944888L;
@JsonIgnore
private String uniqueKey;
@JsonProperty("uniqueKey")
@JsonView(Views.Partner.class)
private String uniqueKeyV2;
@JsonProperty("gtin")
@JsonView(Views.Public.class)
private String gtin;
@JsonProperty("printedGtin")
@JsonView(Views.Public.class)
private String printedGtin;
@JsonProperty("tpnb")
@JsonView(Views.Public.class)
private String tpnb;
// skipped other attributes and getter setter
主要功能如下:
ObjectMapper mapper = new ObjectMapper();
MyProduct product = mapper.readerWithView(Views.Public.class).forType(MyProduct.class).readValue(json)