当我使用MapStruct的qualifiedByName和@Named时不生效

问题描述 投票:0回答:1

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 ]

我确认注释路径是正确的

java mapstruct
1个回答
0
投票

您的情况的问题是

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);
}
© www.soinside.com 2019 - 2024. All rights reserved.