【发布时间】:2020-08-02 23:31:20
【问题描述】:
我正在从 Kotlin 中的服务 A 调用 Java 中的服务 B。它返回一个包含多个字段的对象。 Java 对象中返回的字段之一是枚举。在我的 kotlin 代码中,我定义了一个 DTO,它将返回的响应映射到 kotlin。我需要将此枚举映射到 kotlin 中的字符串值。
Java 中的 DTO:
public class PersonDTO
{
private Long id;
private String name;
private CountryCode countryCode;
}
CountryCode 是一个枚举。
Kotlin 中的数据类:
data class PersonDTO(
val id: Long? = null,
val name: String? = null,
val countryCode: String? = null //How to map the enum to string here..???
)
任何帮助将不胜感激。
【问题讨论】:
-
Kotlin 也有枚举,只需在 Kotlin 代码中导入即可
-
问题是我需要将响应中的枚举字段映射到 kotlin 中的
String类型。刚刚编辑了我的问题。对不起。 -
在枚举上使用
.name()。