我正在尝试使用Jackson 2.1.4将不可变的POJO与JSON序列化,而不必编写自定义序列化程序并尽可能少注释.我还想避免为了满足Jackson库而添加不必要的getter或默认构造函数.
我现在卡在异常上:
JsonMappingException:找不到类型[simple type,class Circle]的合适构造函数:无法从JSON对象实例化(需要添加/启用类型信息?)
代码:
public abstract class Shape {}
public class Circle extends Shape {
public final int radius; // Immutable - no getter needed
public Circle(int radius) {
this.radius = radius;
}
}
public class Rectangle extends Shape {
public final int w; // Immutable - no getter needed
public final int h; // Immutable - no getter needed
public Rectangle(int w, int h) {
this.w = w;
this.h = h;
}
}
Run Code Online (Sandbox Code Playgroud)
测试代码:
ObjectMapper mapper …Run Code Online (Sandbox Code Playgroud)