在尝试POST这个JSON对象数组时,我一直收到错误请求400“抱歉格式不好”:
{
"type":"input","uniqueId":434,"label":"name","viewToCall":"input-configuration-menu"},{"type":"button","uniqueId":930,"label":"name","viewToCall":"button-configuration-menu"}]
我不知道如何在我的@Requestbody中使用不同类型的json对象:
@RequestMapping(value="/saveForm", method = RequestMethod.POST)
public @ResponseBody void saveForm( @RequestBody ArrayList<Components> text ){
do somthing...
}
我找到了这个资源管理器,但我没有经验让它在网络环境中工作: Spring @RequestBody containing a list of different types (but same interface) http://programmerbruce.blogspot.com.es/2011/05/deserialize-json-with-jackson-into.html http://aredko.blogspot.se/2012/04/json-for-polymorhic-java-object.html
答案 0 :(得分:0)
JSON开头缺少[
,否则它是有效的JSON。如果问题不是缺少的尖括号,请将日志放入DEBUG或TRACE中,然后发布堆栈跟踪。
此外,需要使用类似的东西对组件进行注释,以便Jackson知道需要实例化哪个对象(另请参阅Jackson polymorphism without annotations):
@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=As.WRAPPER_OBJECT)
@JsonSubTypes({
@Type(value=Input.class, value="input"),
@Type(value=Button.class, value="button")})
public interface Component {
...
}
@JsonTypeName("type")
public Sub1 implements Component {
}
@JsonTypeName("button")
public Button implements Component {
}