MapStruct版本是
1.5.5.Final
,JDK是17,这是我的代码
@Mapper
public interface UndoIssueConvert {
UndoIssueConvert INSTANCE = Mappers.getMapper(UndoIssueConvert.class);
@Mappings({
@Mapping(target = "undoIssueId", source = "id"),
// @Mapping(target = "createTime", expression = ("java(undoIssue.getUpdateTime() != null ? undoIssue.getUpdateTime() : undoIssue.getCreateTime())"))
@Mapping(target = "createTime", qualifiedByName = "convertCreateTime")
})
UndoIssuePanelRspVO.UndoIssue toUndoIssue(UndoIssue undoIssue);
List<UndoIssuePanelRspVO.UndoIssue> toUndoIssue(List<UndoIssue> undoIssue);
@Named("convertCreateTime")
default Date convertCreateTime(UndoIssue undoIssue) {
return undoIssue.getUpdateTime() != null ? undoIssue.getUpdateTime() : undoIssue.getCreateTime();
}
UndoIssue toUndoIssueEntity(UndoIssueAddUpdateVO addVO);
}
但是我遇到以下异常
java: Qualifier error. No method found annotated with @Named#value: [ convertCreateTime ]
我确认注释路径是正确的
您的情况的问题是
convertCreateTime
方法需要整个 UndoIssue
对象。
您必须直接传递所需字段,而不是将整个
UndoIssue
对象传递给 convertCreateTime
方法。
解决方法如下
更改为
@Mapping(target = "createTime", qualifiedByName = "convertCreateTime")
为 @Mapping(target = "createTime", source = ".", qualifiedByName = "convertCreateTime")
这是更新后的代码,如下所示
@Mapper
public interface UndoIssueConvert {
UndoIssueConvert INSTANCE = Mappers.getMapper(UndoIssueConvert.class);
@Mappings({
@Mapping(target = "undoIssueId", source = "id"),
@Mapping(target = "createTime", source = ".", qualifiedByName = "convertCreateTime")
})
UndoIssuePanelRspVO.UndoIssue toUndoIssue(UndoIssue undoIssue);
List<UndoIssuePanelRspVO.UndoIssue> toUndoIssue(List<UndoIssue> undoIssue);
@Named("convertCreateTime")
default Date convertCreateTime(UndoIssue undoIssue) {
return undoIssue.getUpdateTime() != null ? undoIssue.getUpdateTime() : undoIssue.getCreateTime();
}
UndoIssue toUndoIssueEntity(UndoIssueAddUpdateVO addVO);
}