如何在Kotlin中使用Jackson JsonSubTypes批注 [英] How to use Jackson JsonSubTypes annotation in Kotlin
本文介绍了如何在Kotlin中使用Jackson JsonSubTypes批注的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试转换一些使用Jackson的@JsonSubTypes批注来管理多态性的Java代码.
I'm trying to convert some Java code that uses Jackson's @JsonSubTypes annotation to manage polymorphism.
这是有效的Java代码:
Here is the working Java code:
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = Comment.class, name = "CommentNote"),
@JsonSubTypes.Type(value = Photo.class, name = "PhotoNote"),
@JsonSubTypes.Type(value = Document.class, name = "DocumentNote")
})
public abstract class Note implements Identifiable {
[...]
以下是我认为等同的Kotlin代码:
Here is the Kotlin code I think would be equivalent:
JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "type")
JsonSubTypes(
JsonSubTypes.Type(value = javaClass<Comment>(), name = "CommentNote"),
JsonSubTypes.Type(value = javaClass<Photo>(), name = "PhotoNote"),
JsonSubTypes.Type(value = javaClass<Document>(), name = "DocumentNote")
)
abstract class Note : Identifiable {
[...]
但是在三条"JsonSubTypes.Type"行中的每行上都出现以下错误:
But I get the following errors on each of the three "JsonSubTypes.Type" lines :
Kotlin: An annotation parameter must be a compile-time constant
Kotlin: Annotation class cannot be instantiated
有什么主意吗?
推荐答案
原来是 bug 在编译器中,感谢您对其进行举报.要变通解决此问题,您可以导入JsonSubTypes.Type
并不加限制地使用它:
Turns out it's a bug in the compiler, thanks for reporting it. To work around this issue, you can import JsonSubTypes.Type
and use it without qualification:
import org.codehaus.jackson.annotate.JsonSubTypes.Type
JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "type")
JsonSubTypes(
Type(value = javaClass<Comment>(), name = "CommentNote"),
Type(value = javaClass<Photo>(), name = "PhotoNote"),
Type(value = javaClass<Document>(), name = "DocumentNote")
)
abstract class Note : Identifiable {
[...]
这篇关于如何在Kotlin中使用Jackson JsonSubTypes批注的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文