How to use lombok with default constructor

Pooja Verma :

My AssignedPropertiesDTO class is:

@Data
@Builder
@AllArgsConstructor
public class AssignedPropertiesDTO {

  @JsonProperty("hotel_id")
  private Long hotelId;

  @JsonProperty("oyo_id")
  private String oyoId;

  @JsonProperty("drn")
  private Integer dsrn;

  @JsonProperty("is_sold_out")
  private Boolean isSoldOut;

  @JsonProperty("is_purged")
  private Boolean isPurged;

  AssignedPropertiesDTO() {
    this.isSoldOut = false;
    this.isPurged = false;
  }

}

I need to set isSoldOut and isPurged to false. That is why i have made default constructor. But i am using Builder() for setting class fields and somewhere just setting properties using getter/setter.

  AssignedPropertiesDTO matchingObject = assignedPropertiesDTOS.stream()
      .filter(assignedPropertiesDTO ->
          assignedPropertiesDTO.getHotelId().equals(Long.valueOf(entry.getKey())))
      .findFirst().orElse(null);
  if (matchingObject == null) {
    assignedPropertiesDTOS.add(AssignedPropertiesDTO
        .builder().hotelId(Long.valueOf(entry.getKey())).dsrn(count).build());
  } else {
    matchingObject.setDsrn(count);
  }

My requirement is to set Ispurged/IsSoldOut to either True/False But not NULL.

[      {
                "hotel_id": 45693,
                "oyo_id": "GOA2161",
                "drn": null,
                "is_sold_out": null,
                "is_purged": null
            },
            {
                "hotel_id": 45693,
                "oyo_id": "GOA2161",
                "drn": null,
                "is_sold_out": true,
                "is_purged": false
            } ]

Please guide me how can i do this.

rzwitserloot :

@Builder makes an all-args constructor for you; it does not know about the non-default values required for isSoldOut and isPurged. You can use the @Builder.Default feature for this: @Builder.Default private Boolean isSoldOut = false; for example.

Alternatively just make them lowercase b boolean and 'false' is now the natural default. If that's an option at all, it's by far the best solution here.

Your final option is to make the all-args constructor yourself.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=385755&siteId=1