我有一个接收抽象类的端点:
@RequestMapping(value = URL, method = RequestMethod.POST)
@ResponseBody
public ModelMap update(@RequestBody SomeAbstractClass form) {
...
}
当我发布到它时,我收到错误:
Could not read JSON: Can not construct instance of ...,
problem: abstract types can only be instantiated with dditional type information
我明白为什么会出现此错误,但我不知道在哪里指定附加类型信息
。理想情况下,我可以通过注释之一指定具体类的类型,但此时任何解决方案都足够了。
最佳答案
我相信您正在使用 Jackson 作为 JSON 序列化器。
首先请确保您拥有
<context:annotation-config/>
在你的 spring xml conf 文件中。
我曾经遇到过同样的问题继承类的反序列化我通过注释解决了它
@JsonDeserialize(as=Child.class)
public abstract class AbstractParent { ... }
关于java - 使用抽象类时指定@RequestBody的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14883393/