我想知道是否有办法在Spring3中使用jackson功能将json数据动态解析为不同的对象。
我有一个如下的父类:
public class Recording {
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
两个孩子:
public class Child1Recording extends Recording {
private String program;
public String getProgram() {
return program;
}
public void setProgram(String program) {
this.program = program;
}
}
public class Child2Recording extends Recording {
private String time;
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
}
这样的控制器:
@RequestMapping(value = "/init/postCheck.ajax", method = RequestMethod.POST)
public @ResponseBody
String postCheck(@RequestBody Recording recording) {
if (recording instanceof Child2Recording) {
return "\"child2 success\"";
} else if (recording instanceof Child1ecording) {
return "\"child1 success\"";
}
return "\"only parent Recording\"";
}
我有不同的场景将不同的json数据发布到后端,我想知道是否有办法让控制器像上面说的那样工作?
目前,如果我发送Child2Recording数据,解析时会发生错误。我无法得到我期望的正确对象。