【问题标题】:Create custom annotations for different output from same POJO class为同一 POJO 类的不同输出创建自定义注释
【发布时间】:2018-05-28 12:14:48
【问题描述】:

所以我有一个 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 类,只会为上面写有@public注解的那些字段创建实例。

我需要实现这一点。我认为这可以通过在杰克逊库中创建一些钩子来实现。请问我需要在一天之内完成,谁能指导一下如何做到这一点。

最终产品应该是这样的:

@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 作为唯一属性。

【问题讨论】:

    标签: jackson jackson2


    【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-05
      • 2016-09-04
      • 2010-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-09
      相关资源
      最近更新 更多