如何做龙目岛复合或集料建筑

How to do Lombok compound or aggregate building

这似乎是一个明显的要求,所以我很惊讶没有任何可访问的示例,但我有一个 class 上面有一个 Lombok 构建器注释,其中包含一个 class 上面也有一个 Lombok 构建器,像这样:

@Getter
@Setter
@ToString
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonNaming(value = PropertyNamingStrategy.KebabCaseStrategy.class)
@JsonPropertyOrder({ "priceList", "assetRate", "name", "id", "attributes", "description" })
public class T24Element {
    private T24PriceList priceList;
    private String assetRate;
    private String name;
    private String id;
    @Singular("attribute")
    private List<ReferenceDataItem> attributes;
    private String description;
}

T24PriceList和ReferenceDataItem如下:

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class T24PriceList {
    private PricedItem leaseTermPrice;
    private PricedItem assetFee;
    private PricedItem basePrice;
}
@Getter
@Setter
@ToString
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonDeserialize(builder = ReferenceDataItem.ReferenceDataItemBuilder.class)
@JsonNaming(value = PropertyNamingStrategy.KebabCaseStrategy.class)
@JsonPropertyOrder({ "description", "code", "endDate" })
public class ReferenceDataItem {

    private String description;

    private String code;

    /**
     * Rarely used - seems to be only for leasePeriod reference data
     */
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy")
    private LocalDate endDate;

}

最后,PricedItem 是:

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class PricedItem {
    private String priceType;
    private String matCode;
    private String currencyMnemonic;
    private BigDecimal value;
}

我遇到的问题是,无论我如何使用构建器,我都无法像其他任何东西一样在另一个 classes 构建中获得 @Builder 注释 classes比 null。因此,例如,如果我开始于:

    public static T24Element t24Element = T24Element.builder()
        .priceList(t24PriceList)
        .assetRate("GP")
        .attributes([shortCutKey, coreServiceType, size, sellerPid, leasePeriod, capacity,
                     renewAttribute, dimensions, sellerPid2])
        .id("903551")
        .name("Small Post Office Box")
        .description("Personal mail")
        .build()

    public static T24PriceList t24PriceList = T24PriceList.builder()
        .assetFee(assetFee)
        .basePrice(basePrice)
        .leaseTermPrice(leaseTermPrice)
        .build()

    public static PricedItem leaseTermPrice = PricedItem.builder()
        .priceType("ZPOB")
        .matCode("903551")
        .currencyMnemonic("AUD")
        .value(new BigDecimal("253.92"))
        .build()

    public static PricedItem assetFee = PricedItem.builder()
        .priceType("ZPBF")
        .matCode("903613")
        .currencyMnemonic("AUD")
        .value(new BigDecimal("25"))
        .build()

    public static PricedItem basePrice = PricedItem.builder()
        .priceType("ZPOB")
        .matCode("903551")
        .currencyMnemonic("AUD")
        .value(new BigDecimal("277"))
        .build()

t24PriceList 的值将为空。即使 basePrice 自己正确初始化,当我尝试在以下位置使用该值时:

    public static T24PriceList t24PriceList = T24PriceList.builder()
        .assetFee(assetFee)
        .basePrice(basePrice)
        .leaseTermPrice(leaseTermPrice)
        .build()

它总是空的。看起来 Lombok 看不到聚合 classes 的构建器。我应该在这里做什么?

顺便说一句:我意识到我使用了很多注释,但我一直在尝试不同的组合,例如 @Getter@Setter 而不是 @Data 等等让它工作。

您的静态字段不是 final,这意味着它们在 class 初始化期间为 null 是绝对合法的。在构建 t24PriceList.

时,您的 basePrice 实际上是空的

如果你设置它们static final,如果你在它的声明之前使用常量,编译器应该给你一个警告。或者只是尝试将 basePrice 声明移动到 t24PriceList.

之前