在本文之后https://github.com/FasterXML/jackson-module-scala/wiki/Enumerations
枚举声明为
object UserStatus extends Enumeration {
type UserStatus = Value
val Active, Paused = Value
}
class UserStatusType extends TypeReference[UserStatus.type]
case class UserStatusHolder(@JsonScalaEnumeration(classOf[UserStatusType]) enum: UserStatus.UserStatus)
DTO被声明为
class UserInfo(val emailAddress: String, val userStatus:UserStatusHolder) {
}
并且序列化代码是
val mapper = new ObjectMapper()
mapper.registerModule(DefaultScalaModule)
def serialize(value: Any): String = {
import java.io.StringWriter
val writer = new StringWriter()
mapper.writeValue(writer, value)
writer.toString
}
产生的JSON序列化为
{
"emailAddress":"[email protected]",
"userStatus":{"enum":"Active"}
}
是否有可能获得以下表格?
{
"emailAddress":"[email protected]",
"userStatus":"Active"
}
最佳答案
你有没有尝试过:
case class UserInfo(
emailAddress: String,
@JsonScalaEnumeration(classOf[UserStatusType]) userStatus: UserStatus.UserStatus
)
jackson 维基的例子有点误导。您不需要
holder
类。它只是具有该元素的事物的一个示例。您需要的是注释