提问者:小点点

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


这似乎是一个明显的要求,所以我很惊讶没有任何可访问的示例,但我有一个带有Lombok构建器注释的类,其中包含一个也有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和参考数据项如下:

@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;
}

我遇到的问题是,无论我如何使用构建器,我都无法将其他类中的@Builder注释类构建为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看不到聚合类的构建器。我应该在这里做什么?

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


共1个答案

匿名用户

您的静态字段不是最终,这意味着在类初始化期间它们为空是绝对合法的。在构建t24PriceList时,您的basePrice实际上是null。

如果您将它们设置为静态最终,如果您在其声明之前使用常量,编译器应该会给您警告。或者只是尝试将您的basePrice声明移动到t24PriceList之前。