我有以下类层次结构注释:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes(
JsonSubTypes.Type(value = NetCommand.AddEntity::class, name = "AddEntity"),
JsonSubTypes.Type(value = NetCommand.RemoveEntity::class, name = "RemoveEntity"),
JsonSubTypes.Type(value = NetCommand.MoveEntity::class, name = "MoveEntity"),
JsonSubTypes.Type(value = NetCommand.SpeakEntity::class, name = "SpeakEntity"),
JsonSubTypes.Type(value = NetCommand.AddItem::class, name = "AddItem")
)
sealed class NetCommand {
class AddEntity(val id: Long, val position: TilePosition, val table: Character) : NetCommand()
class RemoveEntity(val id: Long) : NetCommand()
class MoveEntity(val id: Long, val position: TilePosition) : NetCommand()
class SpeakEntity(val id: Long, val username: String, val message: String) : NetCommand()
class AddItem(val id: Long, val item: Item) : NetCommand()
}
我的想法是我可以将一个集合(ArrayList)NetCommand
传递给第二个应用程序,并将它们正确反序列化为适当的子类。
我还编写了一个简单的测试来帮助我迭代注解/杰克逊映射器的不同配置:
val command = NetCommand.AddEntity(1, TilePosition(0, 0), Character.KNIGHT)
val commandList: ArrayList<NetCommand> = ArrayList()
commandList.add(command)
val mapper = jacksonObjectMapper()
val commandListString = mapper.writeValueAsString(commandList)
val resultList = mapper.readValue<ArrayList<NetCommand>>(commandListString)
assert(resultList[0] as? NetCommand.AddEntity != null)
assert((resultList[0] as NetCommand.AddEntity).id == command.id)
这失败了:
val resultList = mapper.readValue<ArrayList<NetCommand>>(commandListString)
出现此错误:
Missing type id when trying to resolve subtype of [simple type, class shared.NetCommand]: missing type id property 'type'
at [Source: (String)"[{"id":1,"position":{"x":0,"y":0},"table":"KNIGHT"}]"; line: 1, column: 51] (through reference chain: java.util.ArrayList[0])
com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Missing type id when trying to resolve subtype of [simple type, class shared.NetCommand]: missing type id property 'type'
at [Source: (String)"[{"id":1,"position":{"x":0,"y":0},"table":"KNIGHT"}]"; line: 1, column: 51] (through reference chain: java.util.ArrayList[0])
任何想法为什么我的类型字段没有被序列化?
(不太理想)解决方案
我找到了一个解决方案,即手动将已初始化的字段添加到具有子类名称的子类主体中。例如。
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes(
JsonSubTypes.Type(value = AddEntity::class, name = "AddEntity"),
JsonSubTypes.Type(value = RemoveEntity::class, name = "RemoveEntity"),
JsonSubTypes.Type(value = MoveEntity::class, name = "MoveEntity"),
JsonSubTypes.Type(value = SpeakEntity::class, name = "SpeakEntity"),
JsonSubTypes.Type(value = AddItem::class, name = "AddItem")
)
sealed class NetCommand { val type: String = javaClass.simpleName }
class AddEntity(val id: Long, val position: TilePosition, val table: Character) : NetCommand()
class RemoveEntity(val id: Long) : NetCommand()
class MoveEntity(val id: Long, val position: TilePosition) : NetCommand()
class SpeakEntity(val id: Long, val username: String, val message: String) : NetCommand()
class AddItem(val id: Long, val item: Item) : NetCommand()
理想情况下,我只想自动使用简单的类名,而不是name = "AddEntity"
在每次JsonSubTypes.Type
调用时使用等。