我有以下POJO:
public interface Shape {
public double calcArea();
public double calcPerimeter();
}
public class Rectangle implement Shape {
// Various properties of a rectangle
}
public class Circle implements Shape {
// Various properties of a circle
}
public class ShapeHolder {
private List<Shape> shapes;
// other stuff
}
Run Code Online (Sandbox Code Playgroud)
我没有问题让GSON序列化ShapeHolder到JSON 的实例.但是当我尝试将该JSON的String反序列化为一个ShapeHolder实例时,我得到错误:
String shapeHolderAsStr = getString();
ShapeHolder holder = gson.fromJson(shapeHodlderAsStr, ShapeHolder.class);
Run Code Online (Sandbox Code Playgroud)
抛出:
Exception in thread "main" java.lang.RuntimeException: Unable to invoke no-args constructor for interface
net.myapp.Shape. Register an …Run Code Online (Sandbox Code Playgroud)