地图枚举到字符串值
我正在使用Mapstruct
的最新版本。我正在尝试将相应字符串值的值映射到实际enum
值。
例如,这是我的enum
:
@Getter
@RequiredArgsConstructor
@Accessors(fluent = true)
public enum Type {
T1("SomeValue"),
T2("AnotherValue");
private final String label;
}
我有一个接收数据的字段/成员java
type(forefertype
) enum
)中的字符串值:somevalue
或entersValue
。然后,我有一个“受控”类型(myType
),并且我想在此中使用实际的枚举常数(t1
或t2
) ,基于发送的字符串值。
我正在寻找一种使用mapsstruct
来执行此操作的方法,因为该应用程序当前将其用于所有映射目的,但是到目前为止,我还找不到实现此目的的方法。
I'm using a recent version of MapStruct
. I'm trying to map the values of the corresponding string values to the actual enum
value.
For instance, this is my enum
:
@Getter
@RequiredArgsConstructor
@Accessors(fluent = true)
public enum Type {
T1("SomeValue"),
T2("AnotherValue");
private final String label;
}
I have a Java
type (ForeignType
) with a field/member that receives the data (one of the string values in the enum
): SomeValue
or AnotherValue
. Then I have a "controlled" type (MyType
) and I would like to use in this one the actual enumeration constant (T1
or T2
), based on string value sent.
I'm looking for a way to use MapStruct
to do this, because the application currently uses it for all mapping purposes, but so far I can't find a way to achieve this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
映射的概念
@valuemapping
,可用于将字符串映射到enum
中。例如,
执行上述映射将为您实现一种方法。但是,另一种方法是使用自定义方法进行映射:
例如
MapStruct has the concept of
@ValueMapping
that can be used to map a String into anEnum
.e.g.
Doing the above MapStruct will implement a method for you. However, an alternative approach would be to use a custom method to do the mapping:
e.g.