2

我有以下类层次结构注释:

@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调用时使用等。

4

2 回答 2

4

我想我已经找到了最好的解决方案。使用JsonTypeInfo.Id.CLASS映射我不再需要为每个子类型提供名称——它只依赖于完全限定的类名。这会自动使用字段名称,我可以使用注释@class自动填充超类以正确命名字段。另外值得注意的是我们根本不需要提供注释。NetCommand@JsonProperty@JsonSubTypes

宁愿使用 SimpleName (例如,AddItem而不是my.fully.qualified.path.AddItem),但还没有弄清楚。

@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY)
sealed class NetCommand { @JsonProperty("@class") val type = javaClass.canonicalName }
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()
于 2018-11-04T03:20:33.307 回答
1

作为 OP 的解决方案和 ryfterek 评论的补充,以下注释将负责明确声明,提到的@JsonProperty("@class") val type = javaClass.canonicalName属性: @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "type"). 其中 'type' 是将在 POJO 中声明的字段的名称。

于 2020-07-12T13:11:18.880 回答