我想将json反序列化为Foo类:

class Foo {
   List<IBar> bars;
}

interface IBar {
   ...
}

class Bar implements IBar {
   ...
}

IBar有两个实现,但是在反序列化时,我总是想使用第一个实现。 (理想情况下,这应该使问题更容易解决,因为不需要运行时类型检查)

我敢肯定我可以编写自定义反序列化器,但是觉得必须有一些简单的方法。

我找到了这个注释,当没有列表时,它可以完美地工作。
@JsonDeserialize(as=Bar.class)
IBar bar;

List<IBar> bars; // Don't know how to use the annotation here.

最佳答案

@JsonDeserialize(contentAs=Bar.class)
List<IBar> bars;

10-05 18:54