【发布时间】:2019-09-27 18:22:41
【问题描述】:
这似乎是一个明显的要求,所以我很惊讶没有任何可访问的示例,但是我有一个带有 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 和 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;
}
我遇到的问题是,无论我如何使用构建器,我都无法将其他类中的 @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 看不到聚合类的构建器。我应该在这里做什么?
顺便说一句:我意识到我使用了很多注释,但我一直在尝试不同的组合,例如 @Getter 和 @Setter 而不是 @Data 等等,以试图让它发挥作用。
【问题讨论】:
-
是什么让你觉得
basePrice会在t24PriceList之前初始化?