英文:
Is there a way of specifying a default input argument for Mapstruct when using multiple source values?
问题
在下面的示例中,我有两个输入对象,映射到一个输出对象。
大多数映射是直接从一个输入到一个输出,只有一个来自另一个对象。
@Mapping(source = "input.a", target = "output.a")
@Mapping(source = "input.b", target = "output.b")
@Mapping(source = "input.c", target = "output.c")
@Mapping(source = "input.d", target = "output.d")
@Mapping(source = "extra.a", target = "output.extraa")
Output toOutputMapper(Input input, ExtraValues extra)
是否有一种方法可以说“使用此对象作为默认对象”,这样就可以省略映射值,放弃 Mapstruct 提供的自动映射呢?
类似于:
@Mapping(source = "extra.a", target = "output.extraa")
Output toOutputMapper(@Default Input input, ExtraValues extra)
英文:
So in the example below, I have 2 inputs object, being mapped to one output object.
The majority of the mappings are direct from one input, to one output, with only one coming from another object.
@Mapping(source = "input.a" target = "output.a")
@Mapping(source = "input.b" target = "output.b")
@Mapping(source = "input.c" target = "output.c")
@Mapping(source = "input.d" target = "output.d")
@Mapping(source = "extra.a" target = "output.extraa")
Output toOutputMapper(Input input, ExtraValues extra)
Is there a way to say "use this object as default", which saves me mapping the values and forgoing the automatic mapping Mapstruct provides?
Something like:
@Mapping(source = "extra.a" target = "output.extraa")
Output toOutputMapper(@Default Input input, ExtraValues extra)
答案1
得分: 1
当使用MapStruct 1.4时,您可以使用映射到当前目标的方式来实现您所寻找的功能。
例如:
@Mapping(source = "input", target = ".")
@Mapping(source = "extra.a", target = "output.extraa")
Output toOutputMapper(Input input, ExtraValues extra)
英文:
When using MapStruct 1.4 you can use mapping to current target to achieve what you are looking for.
e.g.
@Mapping(source = "input" target = ".")
@Mapping(source = "extra.a" target = "output.extraa")
Output toOutputMapper(Input input, ExtraValues extra)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论