使用@JsonProperty批注对JSON模型进行反序列化会更改生成器模式的属性

时间:2019-05-29 13:38:54

标签: java json serialization jackson

我正在编写一个共享对象 ItineraryDataEntry ,该对象正导出到其他项目使用的库中。 模型具有很多属性,因此我正在使用构建器模式方法。 我和QA都在同一个对象上进行反序列化,但是看到的是不同的输入(数据库以字段变量集的形式返回json,而我从api中以更易读的格式返回转换后的json键)。

BE

1)我正在反序列化存储在航空公司数据库中作为json的对象。 然后,我使用 @JsonProperty(“ origin”)转换 field02 来转换该对象的键,并返回 ItineraryDataEntry 作为复杂结构的一部分来自Spring控制器端点的json。 对于这一部分,我没有任何问题。

质量检查

2)质量检查小组正在导入所提及的库,并希望反序列化来自我的API的对此同一个 ItineraryDataEntry 对象的响应。这是一个问题,因为杰克逊不知道如何将响应属性(“ origin”)例如映射回 field02 。他们的解决方案是让我在构建器变量中添加 @JsonProperty(“ origin”)批注。

但是在2)中添加注释提及会破坏反序列化,然后我反序列化与QA团队期望的json不同的json。对于数据库测试,我也需要反序列化。

我如何实现仍然使用带有构建器模式的单个类,并且两个不同的输入都可以正确地反序列化? 作为这个优秀社区的好成员,我能否请您支持?

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

/**
 * Model for Itinerary data item.
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
@ApiModel(value = "ItineraryDataEntry")
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonAutoDetect(getterVisibility = JsonAutoDetect.Visibility.NONE)
@JsonDeserialize(builder = ItineraryDataEntry.ItineraryDataEntryBuilder.class)
public class ItineraryDataEntry implements Serializable {

  @ApiModelProperty(value = "Item type", example = "pnr", allowableValues = "pnr,leg")
  @JsonProperty("type")
  private String type;

  @ApiModelProperty(value = "Item field01", example = "62SVBP")
  @JsonProperty("legNumber")
  private String field01;

  @ApiModelProperty(value = "Item field02", example = "MASON JAMES MR.")
  @JsonProperty("origin")
  private String field02;

  @ApiModelProperty(value = "Item field03", example = "194375957326594")
  @JsonProperty("destination")
  private String field03;

  @ApiModelProperty(value = "Item field04", example = "AIRLINE NAME")
  @JsonProperty("carrier")
  private String field04;

  @ApiModelProperty(value = "Item field05", example = "Y")
  @JsonProperty("fareBasisCode")
  private String field05;

  @ApiModelProperty(value = "Item field06", example = "TRAVEL AGENT NAME")
  @JsonProperty("serviceClass")
  private String field06;

  @JsonIgnore
  private String field07;

  @ApiModelProperty(value = "Item field08", example = "050918")
  @JsonProperty("flightDate")
  private String field08;

  @JsonIgnore
  private String field09;

  @JsonIgnore
  private String field10;

  @ApiModelProperty(value = "Item field11", example = "1115")
  @JsonProperty("departureTime")
  private String field11;

  @JsonIgnore
  private String field12;

  @JsonIgnore
  private String field13;

  @JsonIgnore
  private String field14;

  public String getType() {
    return type;
  }

  public void setType(final String type) {
    this.type = type;
  }

  public String getField01() {
    return field01;
  }

  public void setField01(final String field01) {
    this.field01 = field01;
  }

  public String getField02() {
    return field02;
  }

  public void setField02(final String field02) {
    this.field02 = field02;
  }

  public String getField03() {
    return field03;
  }

  public void setField03(final String field03) {
    this.field03 = field03;
  }

  public String getField04() {
    return field04;
  }

  public void setField04(final String field04) {
    this.field04 = field04;
  }

  public String getField05() {
    return field05;
  }

  public void setField05(final String field05) {
    this.field05 = field05;
  }

  public String getField06() {
    return field06;
  }

  public void setField06(final String field06) {
    this.field06 = field06;
  }

  public String getField08() {
    return field08;
  }

  public void setField08(final String field08) {
    this.field08 = field08;
  }

  public String getField11() {
    return field11;
  }

  public void setField11(final String field11) {
    this.field11 = field11;
  }

  public String getField07() {
    return field07;
  }

  public void setField07(final String field07) {
    this.field07 = field07;
  }

  public String getField09() {
    return field09;
  }

  public void setField09(final String field09) {
    this.field09 = field09;
  }

  public String getField10() {
    return field10;
  }

  public void setField10(final String field10) {
    this.field10 = field10;
  }

  public String getField12() {
    return field12;
  }

  public void setField12(final String field12) {
    this.field12 = field12;
  }

  public String getField13() {
    return field13;
  }

  public void setField13(final String field13) {
    this.field13 = field13;
  }

  public String getField14() {
    return field14;
  }

  public void setField14(final String field14) {
    this.field14 = field14;
  }

  public ItineraryDataEntry(final ItineraryDataEntryBuilder builder) {
    this.type = builder.type;
    this.field01 = builder.field01;
    this.field02 = builder.field02;
    this.field03 = builder.field03;
    this.field04 = builder.field04;
    this.field05 = builder.field05;
    this.field06 = builder.field06;
    this.field07 = builder.field07;
    this.field08 = builder.field08;
    this.field09 = builder.field09;
    this.field10 = builder.field10;
    this.field11 = builder.field11;
    this.field12 = builder.field12;
    this.field13 = builder.field13;
    this.field14 = builder.field14;
  }

  public ItineraryDataEntry() {
  }

  /**
   * The type Itinerary data entry builder.
   */
  @JsonPOJOBuilder(buildMethodName = "build", withPrefix = "")
  public static class ItineraryDataEntryBuilder {

    private String type;

    private String field01;

    private String field02;

    private String field03;

    private String field04;

    private String field05;

    private String field06;

    private String field07;

    private String field08;

    private String field09;

    private String field10;

    private String field11;

    private String field12;

    private String field13;

    private String field14;

    public ItineraryDataEntryBuilder type(final String param) {
      this.type = param;
      return this;
    }

    public ItineraryDataEntryBuilder field01(final String param) {
      this.field01 = param;
      return this;
    }

    public ItineraryDataEntryBuilder field02(final String param) {
      this.field02 = param;
      return this;
    }

    public ItineraryDataEntryBuilder field03(final String param) {
      this.field03 = param;
      return this;
    }

    public ItineraryDataEntryBuilder field04(final String param) {
      this.field04 = param;
      return this;
    }

    public ItineraryDataEntryBuilder field05(final String param) {
      this.field05 = param;
      return this;
    }

    public ItineraryDataEntryBuilder field06(final String param) {
      this.field06 = param;
      return this;
    }

    public ItineraryDataEntryBuilder field07(final String param) {
      this.field07 = param;
      return this;
    }

    public ItineraryDataEntryBuilder field08(final String param) {
      this.field08 = param;
      return this;
    }

    public ItineraryDataEntryBuilder field09(final String param) {
      this.field09 = param;
      return this;
    }

    public ItineraryDataEntryBuilder field10(final String param) {
      this.field10 = param;
      return this;
    }

    public ItineraryDataEntryBuilder field11(final String param) {
      this.field11 = param;
      return this;
    }

    public ItineraryDataEntryBuilder field12(final String param) {
      this.field12 = param;
      return this;
    }

    public ItineraryDataEntryBuilder field13(final String param) {
      this.field13 = param;
      return this;
    }

    public ItineraryDataEntryBuilder field14(final String param) {
      this.field14 = param;
      return this;
    }

    public ItineraryDataEntryBuilder() {
      // prevent init.
    }

    public ItineraryDataEntry build() {
      return new ItineraryDataEntry(this);
    }
  }
}

编辑:一个不理想的解决方案

由于@Coder的建议,我引入了其他变量。 这意味着我们可以使用相同的模型,并且可以按预期对对象进行序列化/反序列化。 QA debug 我已经在这些变量上添加了@JsonIgnore批注,以使响应模型不会更改,但是反序列化现在在两面都有效。

...
  @JsonIgnore
  private String legNumber;

  @JsonIgnore
  private String origin;

  @JsonIgnore
  private String destination;

  public String getLegNumber() {
    return legNumber;
  }

  public void setLegNumber(final String legNumber) {
    this.legNumber = legNumber;
  }

  public String getOrigin() {
    return origin;
  }

  public void setOrigin(final String origin) {
    this.origin = origin;
  }

  public String getDestination() {
    return destination;
  }

  public void setDestination(final String destination) {
    this.destination = destination;
  }
...

Builder part

  public ItineraryDataEntry(final ItineraryDataEntryBuilder builder) {
    this.type = builder.type;
    this.field01 = builder.field01;
    this.field02 = builder.field02;
    this.field03 = builder.field03;
    this.field04 = builder.field04;
    this.field05 = builder.field05;
    this.field06 = builder.field06;
    this.field07 = builder.field07;
    this.field08 = builder.field08;
    this.field09 = builder.field09;
    this.field10 = builder.field10;
    this.field11 = builder.field11;
    this.field12 = builder.field12;
    this.field13 = builder.field13;
    this.field14 = builder.field14;
    this.legNumber = builder.legNumber;
    this.origin = builder.origin;
    this.destination = builder.destination;
    this.carrier = builder.carrier;
    this.fareBasisCode = builder.fareBasisCode;
    this.serviceClass = builder.serviceClass;
    this.flightDate = builder.flightDate;
    this.departureTime = builder.departureTime;
  }
...

    public ItineraryDataEntryBuilder origin(final String param) {
      this.origin = param;
      return this;
    }

    public ItineraryDataEntryBuilder destination(final String param) {
      this.destination = param;
      return this;
    }

    public ItineraryDataEntryBuilder carrier(final String param) {
      this.carrier = param;
      return this;
    }
...

2 个答案:

答案 0 :(得分:0)

我认为您的问题本身就有答案。

  

我正在反序列化一个以json形式保存在航空公司数据库中的对象。然后,我转换该对象的键

不能一步一步完成 ,并且共享相同的模型并对它执行2种不同的反序列化。达到了模型重用的目的。

相反,我建议您使用2个模型。将其反序列化为通常可以在团队/项目之间共享的结构。另一个将其映射到有意义的对象(例如 ItineraryDataEntry ),该对象将具有类型,来源,目的地等。然后,您可以在控制器中编写单独的方法,以在有意义的 ItineraryDataEntry 中转换反序列化的对象。

我假设您的模型比您所提到的要深。在这种情况下,我建议您探索 Orika 库。

答案 1 :(得分:0)

解决方案

由于@Coder的建议,我引入了其他变量。 这意味着我们可以使用相同的模型,并且可以按预期对对象进行序列化/反序列化。 QA debug 我已经在这些变量上添加了@JsonIgnore批注,以使响应模型不会更改,但是反序列化现在在两面都有效。

$(Agent.ReleaseDirectory)

Builder part

...
  @JsonIgnore
  private String legNumber;

  @JsonIgnore
  private String origin;

  @JsonIgnore
  private String destination;

  public String getLegNumber() {
    return legNumber;
  }

  public void setLegNumber(final String legNumber) {
    this.legNumber = legNumber;
  }

  public String getOrigin() {
    return origin;
  }

  public void setOrigin(final String origin) {
    this.origin = origin;
  }

  public String getDestination() {
    return destination;
  }

  public void setDestination(final String destination) {
    this.destination = destination;
  }
...