Neo4J弹簧靴在 kotlin 中带有密封类

问题描述

我正在尝试使用 kotlin 和 neo4j 创建一个有点通用的实现。我现在的想法是我想要一个 GeoNode 可以指向任何类型的 GeoJson 特征(例如“Feature”或“FeatureCollection”)

我尝试使用 kotlins 密封类来做到这一点,例如


@JsonTypeInfo(
    use = JsonTypeInfo.Id.NAME,include = JsonTypeInfo.As.PROPERTY,property = "type"
)
@JsonSubTypes(
    JsonSubTypes.Type(value = GeoJsonFeature::class,name = "GeoJsonFeature")
)
@Node
sealed class FeatureContract(
    @GeneratedValue @Id var id: Long? = null,@Version var version: Long? = null
) {
    companion object {
        @JsonCreator
        @JvmStatic
        private fun creator(name: String): FeatureContract? {
            return FeatureContract::class.sealedSubclasses.firstOrNull {
                it.simpleName == name
            }?.objectInstance
        }

    }
}

data class GeoJsonFeature(
    val geometry: GeometryContract,) : FeatureContract()


data class GeoJsonFeatureCollection(
    val features: List<GeoJsonFeature>,) : FeatureContract()


// and the "GeoNode" that holds this

@Node
data class GeoNode(
    @Id val id: String,@Version var version: Long? = null
    @Relationship(type = "feature") var feature: FeatureContract?,// Should be either a featureCollection or a feature,)

这个想法是我可以在地图上有一个点指向任何种类的 GeoJson。

似乎我成功地将其序列化并在写入时将其放入 Neo4J-db,但是,在读取时我得到

Failed to instantiate [FeatureContract]: Is it an abstract class?; nested exception is java.lang.InstantiationException

Jackson 注释在那里,因为我希望它们能帮助我(Neo4J-OGM 在幕后使用它)但它似乎没有成功。我读过关于 Neo4JEntityConverters 的文章,但我不明白如何为这样的完整对象做到这一点。有没有什么好方法可以在 kotlin 中使用带有 Neo4j-OGM 的密封类进行序列化和反序列化?

使用 spring boot 2.4.4 和 spring-boot-starter-data-neo4j

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)