[英]Mapstruct: Check for null for source before mapping to target
Mapstruct:在映射到目標之前檢查源的 null。 如果源是 null,則需要從 mapstruct 接口拋出自定義異常。
我來晚了一點。 不確定這是否是您正在/正在尋找的:與@Filip 回答中的想法相同,以下對我有用(我正在嘗試設置 LocalDate 類型並在 src 為 null 時拋出):
Mapper 定義了 fcn:
@Named("throwIfNull")
public <T> T throwIfNull(String fieldName) {
throw new RuntimeException("Field: '" + fieldName + "' is null but must not be.");
}
映射調用如下所示:
@Mapping(source = "renewalDate", target = "renewalDate", defaultExpression = "java(throwIfNull(\"renewalDate\"))")
(筆記:
目前不支持此功能。 但是,您可以使用defaultExpression
編寫類似的內容。
無論如何,我不建議為此使用 MapStruct。 Java Bean Validation 更適合執行此操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.