这似乎是一个显而易见的要求,所以令我惊讶的是没有可访问的示例,但是我有一个带有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
,以此类推,这个工作。
答案 0 :(得分:2)
您的静态字段不是final
,这意味着在类初始化期间将它们设为null是绝对合法的。构建basePrice
时,您的t24PriceList
实际上为空。
如果将它们设置为static final
,如果在声明前使用常量,则编译器应向您发出警告。或者只是尝试将basePrice
声明移到t24PriceList
之前。